Search Unity

simple button counter not working?

Discussion in 'Scripting' started by jister, Nov 30, 2012.

  1. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    whats wrong with this... it only works once bothways?

    Code (csharp):
    1. var index = 0;
    2.      var model: GameObject;
    3.          var animName: String[];
    4.      
    5.     if (GUI.Button(Rect((Screen.width/2)+30,25,30,30),">"))
    6.     {
    7.        
    8.         if (index < animName.Length)index ++;
    9.         model.animation.Play(animName[index]);
    10.         print(index);
    11.         print (animName[index]);
    12.        
    13.     }
    14.      
    15.      if (GUI.Button(Rect((Screen.width/2)-30,25,30,30),"<"))
    16.      {
    17.         if (index > animName.Length)index --;
    18.         model.animation.Play(animName[index]);
    19.         print(index);
    20.         print (animName[index]);
    21.      }
     
  2. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    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.
     
  3. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    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.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    If you put those variable declarations inside OnGUI, then they were getting created (and reset) over and over again every frame.

    --Eric
     
  5. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    thx Eric makes sense....