Random animations not working

Hi,

This is my random animation code

       void DeathAnims()
    {

        //int rand = Random.Range(0, anim.Count);
        //Debug.Log(rand);
        //GetComponent<Animation>().Play(anim[rand].name);


        rand = Random.Range(0, 9);
        if (rand == 1)
        {
            hipshakeDeath = true;
            //xploDeath = false;
        }

        if (rand == 2)
        {
            //dead = false;
            xploDeath = true;
        }
        if (rand == 3)
        {
            headSlapDeath = true;
            //xploDeath = false;
        }

        if (rand == 4)
        {
            //dead = false;
            crouchDeath = true;
        }
        if (rand == 5)
        {
            saluteDeath = true;
            //xploDeath = false;
        }

        if (rand == 6)
        {
            //dead = false;
            dance = true;
        }
        if (rand == 7)
        {
            sprald = true;
            //xploDeath = false;
        }
        if (rand == 8)
        {
            dead = true;
            //xploDeath = false;
        }
    }

It’s supposed to randomly choose one of the nine bools that are setup with the animations, but it chooses all at once and activates all the bools. Why does this happen and how do I randomly play one and only one of the nine animations??

We will need more context to find the problem. We need to see how this is being called and what you’re doing with these bools, because as it is now nothing appears to be done with the animations.

I do note in the commented-out code that you’re using the Animation class, which is old and deprecated. It’s been replaced by the Macanim animation system, which uses the class Animator and works differently.

As I mentioned the bools are linked to an animation. In the animator, the link from the “Any state” node to each death animation is connected via each corresponding bool.

So to phrase it differently the question is how do I randomly make 1 of these booleans true? forget the animations. There are 9 bools in total. I’ve tried using Random Range as you can see, but as I wrote before it turns on all of them at once

Do you reset them all to false before randomly activating one? Depending on where you’re calling DeathAnims(), it might activate them all one by one in very few frames, so that it looks like it’s activating them all at once.

Also, a switch() instead of a lot of ifs might be more readable in this case.

1 Like

I set them all to false in the start function. I’m trying your idea using a switch function but I’ve never used them before:

    public int deathAnimations = 8;

          void DeathAnims()
    {  switch (deathAnimations)
        {
            case 1:
                hipshakeDeath = true;
                break;
            case 2:
                xploDeath = true;
                break;
            case 3:
                headSlapDeath = true;
                break;
            case 4:
                crouchDeath = true;
                break;
            case 5:
                saluteDeath = true;
                break;
            case 6:
                dance = true;
                break;
            case 7:
                sprald = true;
                break;
            case 8:
                dead = true;
                break;
         
        }
   }

How do you write random range to randomly choose one of the cases? I think you’re on to something because of the break;

Setting them to false in Start() is not enough if you’re calling DeathAnims() more than once. Without taking a look at the rest of your code (specifically, where and how you’re using DeathAnims) it is pretty much impossible to tell what the problem is.

Using a switch works exactly the same as ifs, only it’s a bit cleaner:

rand = Random.Range(0, 9);
switch (rand)
        {
            case 1:
                hipshakeDeath = true;
                break;
            case 2:
                xploDeath = true;
                break;
            case 3:
                headSlapDeath = true;
                break;
            case 4:
                crouchDeath = true;
                break;
            case 5:
                saluteDeath = true;
                break;
            case 6:
                dance = true;
                break;
            case 7:
                sprald = true;
                break;
            case 8:
                dead = true;
                break;
      
        }
2 Likes

OMG IT WORKS AS IS!!! :smile::smile::smile: Thank you so much Arkano22! I’ve learned something new AND the animations work now yaay :smile:

1 Like

Thats what I wanted to post:
“Your DeathAnim() could also get a random number and pass it into switchcase to choose one of them.” So… gg u were first :smile:

1 Like