simple button counter not working?

whats wrong with this… it only works once bothways?

var index = 0;
	 var model: GameObject;
         var animName: String[];
	 
	if (GUI.Button(Rect((Screen.width/2)+30,25,30,30),">"))
	{
		
		if (index < animName.Length)index ++;
	 	model.animation.Play(animName[index]);
	 	print(index);
		print (animName[index]);
	 	
	}
	 
	 if (GUI.Button(Rect((Screen.width/2)-30,25,30,30),"<"))
	 {
	 	if (index > animName.Length)index --;
	 	model.animation.Play(animName[index]);
	 	print(index);
		print (animName[index]);
	 }

Do you have those Buttons in a OnGUI function?

Also You may need brackets with your conditions. It looks like your trying to include more than one line from you formatting.

Yes the buttons are in OnGui
I moved the model.animation.play(animName[index]); outside the buttons and the index var outside the onGUI. Now it works.
I’m a selftought coder in progress so now i wonder what the difference is for the variable inside a function or outside. It seems that that was what made it work? I only use the var inside the same function i created it so i thought there was no need to set it on top with the rest.

If you put those variable declarations inside OnGUI, then they were getting created (and reset) over and over again every frame.

–Eric

thx Eric makes sense…