- Home /
(Newbie question) why won't my if statement work?
Hello all. I'm very new to Unity and very determined to learn how to use it well. However, while learning the very basics of UnityScript, I've run into a problem. I run the following script in Unity:
#pragma strict
function Start () {
}
var num1 : int = 1;
if (num1 == 1) {
Debug.Log("Yay!");
}
else if (num1 != 1) {
Debug.Log("Nay!");
}
function Update () {
}
When I run it, the console outputs "Nay!" Why is this? Also, if I change the "else if" to "else" it does the same thing. If I remove the "else if" part entirely, it doesn't output anything at all. Does it have anything to do with me leaving the Start and Update functions in there? Should I put the code inside one of those functions? Or is my syntax incorrect? I'm really excited to create things in Unity, so thanks in advance for the help!
Answer by whydoidoit · Mar 15, 2014 at 02:38 PM
In Unity Script you should put your code inside a function - so yep - put it in Awake() or Start() if it's supposed to happen only once when the object is instantiated and included in the scene. (Awake - instantiated, Start - instantiated and became active)