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#
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#
i have tried like this:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class RaceAndAttributeController : MonoBehaviour {
public Color color;
void Start ()
{
color = new Color (0.2f, 0.3f, 0.4f, 0.5f);
GetComponent<Button> ().colors = color;
}
but it gives error:Assets/Scripts/RaceAndAttributeController.cs(13,41): error CS0029: Cannot implicitly convert type UnityEngine.Color' to UnityEngine.UI.ColorBlock’
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().colors to see what’s “inside”.
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)
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class RaceAndAttributeController : MonoBehaviour {
public Color color;
void Start ()
{
color = GetComponent<Button> ().colors.normalColor;
print (color);
}
Here is the answer but its in java. Can anyone help to translate to C#
var colors = GetComponent<Button> ().colors;
colors.normalColor = Color.red;
GetComponent<Button> ().colors = colors;
Ok i figured out this.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class RaceAndAttributeController : MonoBehaviour {
public Color color;
public ColorBlock colorBlockHuman;
public GameObject humanButton;
private Button tempButton;
void Start ()
{
tempButton = humanButton.GetComponent<Button> ();
colorBlockHuman = tempButton.colors;
colorBlockHuman.normalColor = new Color (0.5f, 0.5f, 0.5f, 1.0f);
tempButton.colors = colorBlockHuman;
}