-1

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.

Milan Egon Votrubec
  • 3,696
  • 2
  • 10
  • 24
Tom Jedi
  • 7
  • 1
  • 1
    Are you sure you are referring to the correct `Button` type? Try `UnityEngine.UI.Button` instead – derHugo Feb 07 '23 at 13:11

1 Answers1

0

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

Louis Ingenthron
  • 1,267
  • 8
  • 21
  • When update becomes: // Update is called once per frame void Update() { Button button = t[PlayerPrefs.GetInt(selectedList)].GetComponent – Tom Jedi Feb 07 '23 at 00:27
  • Because the first line should be getting a button and instead it's trying to assign a color. Get rid of everything after GetComponent – Louis Ingenthron Feb 07 '23 at 02:09
  • So now it looks like this: Button button = t[PlayerPrefs.GetInt(selectedList)].GetComponent – Tom Jedi Feb 07 '23 at 05:27
  • Then most likely you're referencing the wrong Button class from the wrong assembly. Check your namespace includes. You should be targeting UnityEngine.UI.Button. – Louis Ingenthron Feb 07 '23 at 22:34