Search Unity

Changing UI Button color C# -Solved-

Discussion in 'Scripting' started by Maxweight, Dec 23, 2014.

  1. Maxweight

    Maxweight

    Joined:
    Nov 2, 2014
    Posts:
    18
    Hi,
    i have tried to change UI buttons color unsuccesfully with scripting also is it possible to change Normal Color, pressed Color.

    Can anyone help? Trying to do it with C#
     
  2. Maxweight

    Maxweight

    Joined:
    Nov 2, 2014
    Posts:
    18
    i have tried like this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class RaceAndAttributeController : MonoBehaviour {
    6.     public Color color;
    7.  
    8.     void Start ()
    9.     {
    10.         color = new Color (0.2f, 0.3f, 0.4f, 0.5f);
    11.         GetComponent<Button> ().colors = color;
    12.     }
    13.  
    but it gives error:Assets/Scripts/RaceAndAttributeController.cs(13,41): error CS0029: Cannot implicitly convert type `UnityEngine.Color' to `UnityEngine.UI.ColorBlock'
     
    Andy0804 likes this.
  3. Marco-Sperling

    Marco-Sperling

    Joined:
    Mar 5, 2012
    Posts:
    620
    the error tells you that "colors" is of type ColorBlock while your color is not.
    You cannot simply assign a different data type to another variable. The data inside needs to match - or you have to cast it on your own.
    ColorBlock probably contains several variables of type Color.
    Add another DOT behind GetComponent<Button>().colors to see what's "inside".
     
    Maxweight likes this.
  4. Maxweight

    Maxweight

    Joined:
    Nov 2, 2014
    Posts:
    18
    ok thx for advice i got bit further now. I still get error:
    NullReferenceException: Object reference not set to an instance of an object
    RaceAndAttributeController.Start () (at Assets/Scripts/RaceAndAttributeController.cs:10)

    why it says nullreference exeption even color has value = RGBA(0.135, 0.167, 0.266, 1.000)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class RaceAndAttributeController : MonoBehaviour {
    6.     public Color color;
    7.  
    8.     void Start ()
    9.     {
    10.         color = GetComponent<Button> ().colors.normalColor;
    11.         print (color);
    12.     }
    13.  
     
  5. Maxweight

    Maxweight

    Joined:
    Nov 2, 2014
    Posts:
    18
    Here is the answer but its in java. Can anyone help to translate to C#
    Code (CSharp):
    1.  
    2.     var colors = GetComponent<Button> ().colors;
    3.     colors.normalColor = Color.red;
    4.     GetComponent<Button> ().colors = colors;
    5.  
    6.  
     
  6. Maxweight

    Maxweight

    Joined:
    Nov 2, 2014
    Posts:
    18
    Ok i figured out this.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class RaceAndAttributeController : MonoBehaviour {
    6.     public Color color;
    7.     public ColorBlock colorBlockHuman;
    8.     public GameObject humanButton;
    9.  
    10.     private Button tempButton;
    11.  
    12.     void Start ()
    13.     {
    14.         tempButton = humanButton.GetComponent<Button> ();
    15.         colorBlockHuman = tempButton.colors;
    16.         colorBlockHuman.normalColor = new Color (0.5f, 0.5f, 0.5f, 1.0f);
    17.         tempButton.colors = colorBlockHuman;
    18.  
    19.     }
    20.