bool not working properly?

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?

    private void Update()
    {
        if (Time.time > nextFlipChance)
        {
            if (Random.Range(0, 10) >= 5)
            {
                flipSlime();
                nextFlipChance = Time.time + flipTime;
                isFlipped = true;
            }
        }
       else
         {
             // isFlipped = false;   <----- This just cancels it out, doesn't trigger the Blink Animation.
          }

        //Animation Control
            if (isFlipped)
            {
                anim.Play(BLINK);
                // isFlipped = false;   <----- This just cancels it out, doesn't trigger the Blink Animation.
            }
            else
            {
               anim.Play(IDLE);
            }
    }

Thanks

to debug put a “print” or “Debug.Log” in your if statement over flipSlime() to check if it gets even called

1 Like

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:

    private void Update()
    {
        if (Time.time > nextFlipChance)
        {
            if (Random.Range(0, 10) >= 5)
            {
                flipSlime();
                nextFlipChance = Time.time + flipTime;
                print("Flipped set to true");
                isFlipped = true;
            }
        }
        else
        {
            print("Flipped set to false");
            isFlipped = false;
        }
        //Animation Control
            if (isFlipped)
            {
                anim.Play(BLINK);
            }
            else
            {
                anim.Play(IDLE);
            }
    }

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.

6614155--752962--Screenshot.jpg

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.

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.

1 Like

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 :stuck_out_tongue: