Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bool keeps reverting back to false

Discussion in 'Scripting' started by Yoshimiii, Jan 28, 2015.

  1. Yoshimiii

    Yoshimiii

    Joined:
    Jun 25, 2014
    Posts:
    6
    The setBarricade() function is called from another class.

    The bool barrierUp is set to true the first time the code runs through the function as it should be, however if I set a breakpoint in the Update() function and continue, the value for barrierUp is false and the line animation.Play ("WallOff Animation"); never gets executed.

    Any help is appreciated. Thanks.

    Code (CSharp):
    1. void Update () {
    2.         foreach (AnimationState state in animation) {
    3.             switch (animation.name) {
    4.             case "WallOff": //Different animations here, use directory name of animation
    5.  
    6.                 if (barrierUp)
    7.                 animation.Play ("WallOff Animation");
    8.  
    9.                 break;
    10.              
    11.             }
    12.         }
    13.     }
    Code (CSharp):
    1. public void setBarricade()
    2.     {
    3.         barrierUp = true;
    4.     }
     
  2. tobyheadcast

    tobyheadcast

    Joined:
    Aug 5, 2014
    Posts:
    25
    Bit tricky without seeing the context. Where is barrierUp defined? When is it set true and from where? Are there any code paths that can set it to false?
     
  3. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    You're absolutely sure that's why your animation isn't playing? Have you checked to see if the switch statement itself is executing?
     
  4. Yoshimiii

    Yoshimiii

    Joined:
    Jun 25, 2014
    Posts:
    6
    barrierUp is defined in same class and set to false in void Start().

    setBarricade() is only function that sets it to true. It's set to true once the user clicks on a button. No other code that can set it to false.

    Other class:

    Code (CSharp):
    1. public void ClickBarricades(L1GameMode d)
    2.     {
    3.         d.setBuyMenuText (false);
    4.  
    5.         animator.setBarricade ();
    6.     }
     
  5. Yoshimiii

    Yoshimiii

    Joined:
    Jun 25, 2014
    Posts:
    6
    Nvm. Figured it out. It was to do with my inspector and not dragging the correct script onto game object.
     
    Last edited: Jan 28, 2015