Odd variable issues

So this is kinda wierd. But I have a defined variable called face at the start of my script. Simple “var face=1;” which works.

However, I created a function called “changeface”. Inside this function I have it check the face variable and +=1 or -=1 like so. In the right button I use this.
if(face < 3) {
face += 1;
}
Debug.Log(face);

I then do the obvious opposite for left. And when I run and test them, it works showing it add 1 all the way up to 3. But when I’m at 3 and hit the left it imedietly goes to 1 and then neither button works. Not sure why, maybe it’s something with the event triggers in unity 5?

Sorry if there are any autocorrects or lack of info. Hard to write from my phone.

The problem wasn’t part of the code you provided, so it must be in how you were changing the variables in your other script. To get it working, these are the steps I followed:

  1. Attach your script to an object in the scene. I used the canvas for my tests

  2. Change your ChangeFace function to

    function ChangeFace ( num : int)
    {
    var temp = face + num;

     if ( !(temp < 1 || temp > 3 ))
     {
     	face = temp;
     }
     Debug.Log(face);
    

    }
    This is not absolutely necessary, but it removes the need to track two bools.

  3. Set up your left and right buttons to point to the ChangeFace function with the OnClick event in the inspector. As the int argument for the right button, use 1. For the left button, use -1.

That should do it. Good luck.

var face = 1;
var isLeft = false;
var isRight = false;

function ChangeFace () {
   if(isLeft){
      if(face > 1){
         face -= 1;
      }
   }
   if(isRight){
      if(face < 3){
         face += 1;
      }
   }
   Debug.Log(face);
}

Hopefully that came out right. I had to rewrite it on my phone for this reply since my computer’s not attached to the Internet ATM. But I have isLeft and isRight selected on the appropriate buttons in my inspector. It almost works, but then goes buggy as heck.