- Home /
Need help with If/else statement.
Hey there.
I am trying to make a script which plays the appropriate animation depending on a buttons state. If its on ,it switches off ,and is its off ,it plays the animation backwards. Now ,i have a problem where in an IF / ELSE statement (which i am new to) wont work as i intend it to. The function "Switch" gets called with a raycast ,and it works ,the on animation plays, but once i click it agan ,the same animation plays from frame 1. Here is the code:
#pragma strict
var state : int;
function Start(){
var state = 0;
}
function Switch () {
if (state == 0){
Debug.Log("On");
animation.CrossFade("push");
var state = 1;
Debug.Log(state);
}
else {
Debug.Log("Off");
}
}
So ,the animation plays, and the Debug.Log returns 1 ,so the change occured. Why on Earth wont it work? Its probably some obvious rookie mistake ,but i cant figure it out, so any help would be greatly appriciated.
Thanks.
Answer by PAEvenson · Sep 26, 2012 at 08:40 PM
you are declaring local state variables. For example var state = 1; in the Switch function is changing the local variable. Remove the var from the Start and Switch functions. You should be assigning the variable not creating a new one. state = 1;
Your answer
Follow this Question
Related Questions
{ ERROR } call a function from other script 1 Answer
How to check if a function is called? 2 Answers
Call GUITexture and script 1 Answer
Return multiple parameter 3 Answers
Call a function multiple times 2 Answers