Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unity UI [Solved - bug] Unity 2019.2 - change the Button normalColor via script?

Discussion in 'UGUI & TextMesh Pro' started by ivank, Apr 30, 2020.

  1. ivank

    ivank

    Joined:
    Nov 16, 2013
    Posts:
    105
    Hello,
    I would appreciate help with this probably trivial problem:
    I need to change the Normal Color (or Highlighted, Pressed, whatever...) of the button (see attached image) *from the script*.
    Till the 2019.1 it could be done easily by accessing the buttons set of colors (in the form of ColorBlock) and change it there like this:

    Code (CSharp):
    1. ColorBlock allButColors = myButton.GetComponent<Button>().colors;
    2. allButColors.normalColor = myDesiredColor;
    3. myButton.GetComponent<Button>().colors = allButColors;
    However since 2019.2 the above method seems impossible, so I guess it has been replaced by (hopefully) even easier way - could anybody provide any advice which one? :)
    I have searched so far without luck...

    The button setup (quite standard) as shown in the Inspector is at the image below:
    buttonInspector.jpg

    Edit:
    Of course the above method has not changed and should work.
    I wrongly assumed that it was changed because my Visual Studio Code for unknown reason could not find UnityEngine.UI, needed for using ColorBlock etc.
    I found several posts mentioning this, so it is already known problem.

    In my case it helped to change "Project Settings -> Player -> API Compatibility Level" from the original ".NET Standard 2.0" to ".NET 4.x". Not quite sure why :)
     
    Last edited: May 6, 2020