- Home /
Object color change
Hello
I am trying to change my game object color through script, I have defined my game object and my buttons as game objects, but when I click on them, color of game object does not change.
public class ChangeColor : MonoBehaviour {
// Define C911B game object
private GameObject C911B = new GameObject();
// Define buttons
private GameObject turnred = new GameObject();
private GameObject turnblack = new GameObject ();
private GameObject turnwhite = new GameObject ();
private GameObject turnyellow = new GameObject ();
// Use this for initialization
void Start () {
GameObject.Find ("911B").renderer.enabled = true;
GameObject.Find ("turn-red").renderer.enabled = true;
GameObject.Find ("turn-black").renderer.enabled = true;
GameObject.Find ("turn-white").renderer.enabled = true;
GameObject.Find ("turn-yellow").renderer.enabled = true;
C911B = GameObject.Find ("911B");
turnred = GameObject.Find ("turn-red");
turnblack = GameObject.Find ("turn-black");
turnwhite = GameObject.Find ("turn-whit");
turnyellow = GameObject.Find ("turn-yellow");
}
// Update is called once per frame
//void Update () {
//
//}
void OnMouseClick ()
{
if (turnred) {
C911B.renderer.material.color = new Color (232, 0, 0, 255);
} else if (turnblack) {
C911B.renderer.material.color = new Color (0, 0, 0, 255);
} else if (turnwhite) {
C911B.renderer.material.color = new Color (184, 184, 184, 255);
} else if (turnyellow) {
C911B.renderer.material.color = new Color (201, 187, 0, 255);
}
}
}
turnred is a gameobject, but you're treating it like a Boolean:
if (turnred) {
@Seyed$$anonymous$$ahdi : Why did you ask this again? http://answers.unity3d.com/questions/746892/cs0029-convert-type.html
@Lo0Nuhti$$anonymous$$ He probably didn't know asking questions with that reputation requires approval.
Nah... the other question has been posted for about a day, and this question's content has the changes to the script that someone else showed in the other topic ;) ..tags are all "help help help" also, so Seyed is just being impatient lol
Thank you for comments, because I dont get working answer in one post, I post it with changes to see if other people help @Lo0Nuhtik and @YoungDeveloper
Your answer
![](https://koobas.hobune.stream/wayback/20220613153947im_/https://answers.unity.com/themes/thub/images/avi.jpg)