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 not working properly?

Discussion in 'Scripting' started by anglojojo, Dec 12, 2020.

  1. anglojojo

    anglojojo

    Joined:
    Nov 11, 2020
    Posts:
    18
    Hi,

    I've been trying to figure out why my bool isn't working properly. I am pretty new to coding.

    All I'm trying to do is set a bool as a trigger for playing an Animation.
    The bool I'm having issues with is called "isFlipped".
    Note: I don't initialise the bool at the start function, I just declare it below the class.

    I tried placing the false statement in various places, but it just keeps cancelling it out, and the BLINK animation is not triggered. Can anyone help please?

    Code (CSharp):
    1.     private void Update()
    2.     {
    3.         if (Time.time > nextFlipChance)
    4.         {
    5.             if (Random.Range(0, 10) >= 5)
    6.             {
    7.                 flipSlime();
    8.                 nextFlipChance = Time.time + flipTime;
    9.                 isFlipped = true;
    10.             }
    11.         }
    12.        else
    13.          {
    14.              // isFlipped = false;   <----- This just cancels it out, doesn't trigger the Blink Animation.
    15.           }
    16.  
    17.         //Animation Control
    18.             if (isFlipped)
    19.             {
    20.                 anim.Play(BLINK);
    21.                 // isFlipped = false;   <----- This just cancels it out, doesn't trigger the Blink Animation.
    22.             }
    23.             else
    24.             {
    25.                anim.Play(IDLE);
    26.             }
    27.     }
    Thanks
     
  2. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    to debug put a "print" or "Debug.Log" in your if statement over flipSlime() to check if it gets even called
     
    anglojojo likes this.
  3. anglojojo

    anglojojo

    Joined:
    Nov 11, 2020
    Posts:
    18
    Thanks for the input, did some testing, seems like the varibale is working properly just very fast which doesn't show in the inspector debug which initially made me think its not working.

    I've set the code as follows:
    Code (CSharp):
    1.     private void Update()
    2.     {
    3.         if (Time.time > nextFlipChance)
    4.         {
    5.             if (Random.Range(0, 10) >= 5)
    6.             {
    7.                 flipSlime();
    8.                 nextFlipChance = Time.time + flipTime;
    9.                 print("Flipped set to true");
    10.                 isFlipped = true;
    11.             }
    12.         }
    13.         else
    14.         {
    15.             print("Flipped set to false");
    16.             isFlipped = false;
    17.         }
    18.         //Animation Control
    19.             if (isFlipped)
    20.             {
    21.                 anim.Play(BLINK);
    22.             }
    23.             else
    24.             {
    25.                 anim.Play(IDLE);
    26.             }
    27.     }
    The bool is being set properly so it seems:

    The issue seems to be with the BLINK animation not being played fully. As the bool is set to true very shortly so does the animation, I need to figure out now how to play the animation full length before transitioning to any other state machine. Any clues? I tried PlayFixedTime, but didn't work for me.
     

    Attached Files:

  4. anglojojo

    anglojojo

    Joined:
    Nov 11, 2020
    Posts:
    18
    Final update:
    So in case anyone wondered I got it to work finally. Once I confirmed bool is functioning properly, I add the following code for the BlLINK animation to ensure its played fully from start to finish before transitioning to any other state machine.

    Code (CSharp):
    1. Animator.PlayInFixedTime(BLINK, -1, 0);
    I got confused at first with the 3rd parameter of the FixedTime function. It seems to be the beginnig offset rather than the duration as I initially thought lol but this only works if I have a "Transtion" set back to the StateMachine IDLE in my case, with settings as: No transition duration, Has Exit time, Fixed time and no loop.

    Would be nice to do it without have a transition set via the Animator GUI on Unity.
     
    Kurt-Dekker likes this.
  5. anglojojo

    anglojojo

    Joined:
    Nov 11, 2020
    Posts:
    18
    Real final update lol (to help others)

    So after playing around with the Scripts, it seems PlayInFixed time is not helpful, as once its played, I can no longer switch Animation states from the Script using the Play() function, which was very odd. It would relay purely on the Transition set up in the Animator GUI on Unity.

    Anyhow, I ended up just setting a timer variable set at the length of the Animation, so the other state could only switch if the timer runs out. This worked like a charm, because now I don't even need to set up a Transition in the GUI hehe :p