- Home /
CS0029 convert type
Hi
I am trying to change the color of my gameobject called p911 to specific colors by pushing buttons that I defined them in script, but I get the error that cannot convert string to bool. if anyone that can help I would appreciate it. if there are any problems in my other parts of code, please tell me.
thank you.
public class ChangeColor : MonoBehaviour {
private GameObject P911 = new GameObject();
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 ("P911").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;
P911 = GameObject.Find ("P911");
turnred = GameObject.Find ("turn-red");
turnblack = GameObject.Find ("turn-black");
turnwhite = GameObject.Find ("turn-whit");
turnyellow = GameObject.Find ("turn-yellow");
}
void OnMouseDown ()
{
//P911.gameObject.renderer.material.color = new Color (r, g, b, a);
gameObject.renderer.material.color = new Color (11,22,33,123);
if ("turn-red") {
P911.renderer.material.color = new Color (232, 0, 0, 255);
} else if ("turn-black") {
P911.renderer.material.color = new Color (0, 0, 0, 255);
} else if ("turn-white") {
P911.renderer.material.color = new Color (184, 184, 184, 255);
} else if ("turn-yellow") {
P911.renderer.material.color = new Color (201, 187, 0, 255);
}
}
}
Answer by Kiwasi · Jul 12, 2014 at 08:16 PM
Your ifs should be using the variable, not the literal string. As follows.
if (turnred) {
P911.renderer.material.color = new Color (232, 0, 0, 255);
} else if (turnblack) {
P911.renderer.material.color = new Color (0, 0, 0, 255);
} else if (turnwhite) {
P911.renderer.material.color = new Color (184, 184, 184, 255);
} else if (turnyellow) {
P911.renderer.material.color = new Color (201, 187, 0, 255);
}
This will fix the error, but not sure if it will behave how you intend.
can this be done by switch case ? if it can, what should I declare the type of switch case variable ?
can this be done by switch case ? if it can, what should I declare the type of switch case variable ?
First thank you for you help It fixed the problem but does not change the color of object.
I told you this would fix your error, but not behave how you intended. This is a really messed up way to do things.
Try using OnGUI and GUI.Button ins$$anonymous$$d.
Your answer
![](https://koobas.hobune.stream/wayback/20220613153910im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Change color of object 1 Answer
Object color change 0 Answers
Displaying object with button 1 Answer
How to make a camera follow an object 0 Answers