Change colour of a button through C# script in Unity - c#

Everywhere online says it is possible to change the 'normalColor' variable of a button in Unity. It is changeable in the inspector, but for whatever reason, when I run the following code:
public class ButtonSelector : MonoBehaviour
{
public string selectedList;
Transform[] t;
void Start()
{
t = GetComponentsInChildren<Transform>();
}
void Update()
{
t[PlayerPrefs.GetInt(selectedList)].GetComponent<Button>().colors = new Color32(191, 255, 203, 255);
}
}
It throws this error:
'Button' does not contain a definition for 'colors' and no accessible extension method 'colors' accepting a first argument of type 'Button' could be found (are you missing a using directive or an assembly reference?).
I am using Unity2021.3.5f1, I don't want to change the Image colour, I specifically want to change the normalColor variable of the button.
I have tried .colors, .color, .spriteState, all of which are not found inside the button class.

Button.colors is a ColorBlock struct, not a Color.
You need to either create your own ColorBlock, or get the current one from the button, make your modifications, and save the block back to the button.
See here for more information: https://docs.unity3d.com/Packages/com.unity.ugui#1.0/api/UnityEngine.UI.Selectable.html#UnityEngine_UI_Selectable_colors

Related

Trying to get a variable form a script in public gameobject, error CS1061

I am trying to get a variable "isOffRoad" from script "WheelHandler" in the object "Wheel1".
I am getting the error:
Assets\Scripts\CarControler.cs(53,13): error CS1061: 'GameObject' does not contain a definition for 'WheelHandler' and no accessible extension method 'WheelHandler' accepting a first argument of type 'GameObject' could be found (are you missing a using directive or an assembly reference?)
Thanks in advance.
Edit: isOffRoad is public.
public GameObject Wheel1;
void Update()
{
if(Wheel1.WheelHandler.isOffRoad == true)
{
Debug.Log("offroad");
}
}
You need to access your component with GetComponent<>() method.
In your case that would be Wheel1.GetComponent<WheelHandler>().isOffRoad
Also It is not recommended to access component in Update loop. It would be better to cache it in a field.
Try this one:
if (Wheel1.GetComponent<WheelHandler>().isOffRoad))
{
// do something..
}

Error after trying to give GUI different text

I am currently making a game where you need to collect coins. I want to make a gui that shows the amount of coins you have. But every time when I press save it shows this error:
"error CS1061: 'Text' does not contain a definition for 'Text' and no accessible extension method 'Text' accepting a first argument of type 'Text' could be found (are you missing a using directive or an assembly reference?)"
I have the code below. Please help!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GuiScript : MonoBehaviour
{
public int score;
public Text m_Label;
//Update is called once per frame
void Update()
{
score = GlobalVariables.score;
m_Label.Text = score;
}
}

Unity C# interfaces causing "script class cannot be found"

I am using Unity 2019.3.13f1 and Visual Studio Community 2019 version 16.5.4. I have the script InterfaceContainer.cs as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InterfaceContainer : MonoBehaviour {
// Start is called before the first frame update
void Start() {
}
// Update is called once per frame
void Update() {
}
}
public interface IItem {
public string Name { get; }
public string Path { get; }
public GameObject Icon { get; }
public void Open();
}
Visual Studio gives no compilation errors.
In Unity the inspector of the script says "No MonoBehaviour scripts in the file, or their names do not match the file name." And when I drag the script into a GameObject, it says "Can't add script component "InterfaceContainer" because this script class cannot be found. Make sure that there are no compile errors and that the file name and the class name match."
The names definitely match, because when I deleted the interface part the error didn't exist anymore.
I also tried deleting the class part. It didn't help.
Any subsequent scripts added had the exact same error, whether or not they contain any interfaces, or reference this script.
The weird thing is when I deleted the interface part of this script, refreshed Unity, added this part again, and refreshed Unity again, the error disappears. However all subsequently added scripts still have the same error.
I have no idea what causes this error, and have googled for a long time with no avail. Any help will be greatly appreciated.
EDIT: The error isn't gone when I removed the interface part and added it again; I can still drag the script as a component, when when I try entering playmode it asks me to fix all compiler errors.
Try this:
Right click in the Project Panel of Unity
Import New Asset... and select InterfaceContainer.cs
Try add this script as component again.
One thing I would try is to check if it happens in earlier versions of unity. With the unity hub its easy to have different versions of unity to check this kind of things. Its useful if you are working with one of the latests.
I can confirm that in 2018.4.12, I attach my monobehaviours with interfaces with no problem.
On the other hand you dont implement the Interface you define in your monobehaviour, although that should not be causing any problem, have you tried if implementing the interface in your monobehaviour if you got the error?
Hope that helps

How to fix 'The type or namespace name could not be found ' error in unity?

I am setting up a new scriptable object in Unity 5 and when I am trying to set up a reference to it an error shows up :'The type or namespace name 'ES' could not be found (are you missing a using directive or an assembly reference?'
the scriptable object script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "new ES", menuName = "ES")]
public class LAMP: ScriptableObject {
public int groupNum;
void Start() {
groupNum = 1;
}
}
the reference in monobehavior script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bout: MonoBehaviour {
public ES et;
// Use this for initialization
void Start() {
}
// Update is called once per frame
void Update() {
}
}
It is a name space error ,if you are using a collision variable you face this error.
Using My.Players.namespace;
Just had this error and this is the top SO post about it, so I'll post my fix here:
We introduced unit tests, which require the .asmdef for each "group" of related scripts. In this particular instance for us, the original developer had the .asmdef file for scripts that were contained in an AutoGenerated folder (ROS messages) that was set to be ignored by Git.
He pushed the code, Git ignored the .asmdef file for the AutoGenerated files, and then because we were using .asmdef files and that folder wasn't included on MY box, my Unity "couldn't find" the files there.
This could be your problem, too, but 99 times out of 100 you get this error because you've misspelled the class name - capitalization matters! In OP's case specifically the actual class is LAMP even though it's called ES in the AssetMenu. Instead of public ES et; OP should have had public LAMP et; and then it would work.
In your file Bout.cs you are trying to create the variable 'et' of type ES but the type ES doesn't exist, you may want to create a LAMP?
public class Bout: MonoBehaviour {
public LAMP et;
// Use this for initialization
void Start() {
Creating a scriptable object file named ES (fileName = "new ES") doesn't mean it's type is ES, it takes the type of the class i.e LAMP
I have encountered the same error while trying to add script component to the gameobject on runtime. Fixed it by following this format:
GameObject.AddComponent(typeof(namespace.className));
The meaning of The type or namespace name 'xxxx' could not be found is because you are trying to use a class that do not exist or you are not using the namespace where that class is.
The problem that you have is that you are trying to use something that do not exists.
You are trying to create a scriptable object named ES, but it's totally different to create a class named ES.
If you create the class ES you will see that the error goes away.

Defining a symbol for Conditional attribute in Unity3D

[EDIT]
For someone who came across this page for some reason,
things have changed now. Starting Unity 2017, you have to define the same symbol in both files(Callee and Caller) regardless of the file location.
Below was the issue before Unity 2017.
I'll leave this for the record.
In standard C#, if I want to call a method with a conditional attribute, I have to define a symbol for it in the file where the method is CALLED. But in Unity, it seems different. It only works when I put the line in the file where the method EXISTS. Let's say, there are two script files in a Unity project like this.
ClassForMySymbol.cs
class ClassForMySymbol {
[System.Diagnostics.Conditional("MY_SYMBOL")]
public static void Print() {
Debug.Log ("This method is called.");
}
}
Test.cs
public class Test : MonoBehaviour {
void Start() {
ClassForMySymbol.Print();
}
}
This will only work when I define "MY_SYMBOL" in ClassForMySymbol.cs but not Test.cs.
I don't get what's happening. Is this normal? What am I missing here?
PS. This question is not about #define's scope or defining a global symbol.

Categories

Resources