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){
	var state = 1;
else {


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.


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;