Script will not change animation parameter

HI, I am trying the animation component out and I have my animation set up and code to change the parameter of it, but nothing seems to happen. If I run the game and click the parameter in the animator tab, the animation changes, but when I run the game, and attempt to do the actions that should make the animation run, it doesnt work.

This is the code and a image of the animation set up, if someone could possibly help me out. thanks

    Animator DeathAnimator;

  void Update()
    {

        if (Health <= 0)
        {            
            GameObject.FindWithTag("MainCamera").GetComponent<EnemySpawner>().EnemyCounter -=1;
            //Destroy(this.gameObject);
            //Debug.Log("Enemies Alive" + EnemyCounterScript);
            DeathAnimator.SetBool("DemonDeath", true);
            if (Random.Range(0f, 1f) <= DropRate)
            {
               Instantiate(HealthPickUp, transform.position, transform.rotation);
            }
        }

       
    }

1 Like

First, avoid GameObject.find calls. There are better ways to do it such as adding a public gameObject to your script and dropping the camera in there. For this case it isn’t as big as deal since you are calling it once but it’s good coding habit to avoid.

Your animation setup looks correct from what I can tell. The culprit may be the Health int. How are you retrieving Health and have you done a Debug.Log(Health) to make sure Health <=0? I would start there first.

Hey, The health is working fine, i checked that part out. Could it be because the the object that the animation relates to is being instantiated after the scene starts, and is not in the game to start?

As long as you are setting demondeath bool true after it is instantiated then no that is not the problem. If however it isn’t instantiated prior to setting the bool to true then yes that is your problem since your default setting is false. Is the script attached to the instantiated object? I would assume yes since I don’t see a getcomponent call.

Yup the Script is attached to the object, and the bool is called after the object is instantiated.

Wait, I just saw you don’t have a getcomponent call for DeathAnimator.

Add this,

public void start()
{
DeathAnimator = GetComponent<Animator>();
}
1 Like

Thanks man, u saved me!!