I have a script that I need to make 3 objects cycle through. Here's my script... Code (JavaScript): var radio : GameObject; var lighter : GameObject; var pipe : GameObject; function Start () { radio.SetActive (false); lighter.SetActive (false); pipe.SetActive (false); } function Update () { if (Input.GetButtonUp("Fire2") && Input.GetAxis ("Vertical") > 0.5) lighter.SetActive (true); radio.SetActive (false); pipe.SetActive (false); if (Input.GetButtonUp("Fire2") && Input.GetAxis ("Vertical") < 0.5) radio.SetActive (false); pipe.SetActive (false); lighter.SetActive (false); if (Input.GetButtonUp("Fire2") && Input.GetAxis ("Horizontal") > 0.5) pipe.SetActive (true); radio.SetActive (false); lighter.SetActive (false); if (Input.GetButtonUp("Fire2") && Input.GetAxis ("Horizontal") < 0.5) pipe.SetActive (false); radio.SetActive (true); lighter.SetActive (false); } But I have a few issues 1. the radio variable starts enabled, and won't switch off 2. the pipe variable is the only one that works. I don't understand why. Could someone help?
You're missing some brackets, there! UnityScript (and C#) does not care about where your code is tabbed to, so even if you write it like this: Code (csharp): if (Input.GetButtonUp("Fire2") && Input.GetAxis ("Horizontal") < 0.5) pipe.SetActive (false); radio.SetActive (true); lighter.SetActive (false); It works like this: Code (csharp): if (Input.GetButtonUp("Fire2") && Input.GetAxis ("Horizontal") < 0.5) { pipe.SetActive (false); } radio.SetActive (true); lighter.SetActive (false); Ie. every line not directly below one of your if-statements is always run. You need this: Code (csharp): if (Input.GetButtonUp("Fire2") && Input.GetAxis ("Horizontal") < 0.5) { pipe.SetActive (false); radio.SetActive (true); lighter.SetActive (false); }
Code (csharp): // no curly brackets if() commandControlledByIf commandNotControlledByIf commandNotControlledByIf // curly brackets if() { commandControlledByIf commandControlledByIf commandControlledByIf }
Okay, I did that, and now it starts okay with everything equaling false, but when I hold down and then let go of Fire2, the pipe variable turns on no matter what axis is held, or if they're even held at all. After that, it doesn't turn off. What's wrong now? Code (JavaScript): var radio : GameObject; var lighter : GameObject; var pipe : GameObject; function Start () { radio.SetActive (false); lighter.SetActive (false); pipe.SetActive (false); } function Update () { if (Input.GetButtonUp("Fire2") && Input.GetAxis ("Horizontal") > 0.5) { lighter.SetActive (true); radio.SetActive (false); pipe.SetActive (false); } if (Input.GetButtonUp("Fire2") && Input.GetAxis ("Vertical") < 0.5) { radio.SetActive (false); pipe.SetActive (false); lighter.SetActive (false); } if (Input.GetButtonUp("Fire2") && Input.GetAxis ("Horizontal") < 0.5) { pipe.SetActive (true); radio.SetActive (false); lighter.SetActive (false); } if (Input.GetButtonUp("Fire2") && Input.GetAxis ("Vertical") > 0.5) { pipe.SetActive (false); radio.SetActive (true); lighter.SetActive (false); } } Also, the code is in JavaScript. I saw you mentioned CSharp in your response, and I just wanted to make sure you knew.