-1
\$\begingroup\$

In my game I am trying to trigger the death animation but it doesn't work when I use anim.SetBool("isDead", true); however when I use GetComponent<Animator>().SetBool("isDead", true); it works. I initialized anim by doing this: private Animator anim; and in Start anim = this.gameObject.GetComponent<Animator>();. Then I did the previous code and only the getcomponenet works. When I do anim.SetBool("isDead", true); it gives an NullRefrenceExeption Errror. The isDead bool is a actual variable in the animator.

This is the script: (The important parts will have comments beside them saying important)

using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine;
using UnityEngine.AI;

public class Enemy
{
    public float health;
    public float speed;
    public NavMeshAgent agent;
    public float inRange;
    public float inView;
    

    public Enemy(float health, float speed, NavMeshAgent agent, float inRange, float inView)
    {
        this.health = health;
        this.speed = speed;
        this.agent = agent;
        this.inRange = inRange;
        this.inView = inView;
        
    }
}

public class EnemyBaseScript : MonoBehaviour
{
    public float health;
    public float speed;
    public NavMeshAgent agent;
    public float inRange;
    public float inView;    
    public Enemy enemy;
    public GameObject self;
    public bool MultiAttack = false;

    public GameObject playerPos;

    private float SelectedDif;
        
    private Animator anim; //important

    private PlayerPos pl;
    private Weapon weaponGettingHitBy;
    private TakeDamage tk;
    private TimeToFight TtF;
    private Difficulty dif;
    private GameObject buttonController;

    // Start is called before the first frame update
    void Start()
    {
        pl = GetComponent<PlayerPos>();
        tk = GetComponent<TakeDamage>();
        TtF = GetComponent<TimeToFight>();
        dif = GetComponent<Difficulty>();
        buttonController = GameObject.FindGameObjectWithTag("ButtonController");

        SelectedDif = buttonController.GetComponent<Difficulty>().difficulty;
        SceneManager.UnloadSceneAsync("DifficultyChosing");

        health = health * SelectedDif;
        speed = speed * SelectedDif;
        inRange = inRange * SelectedDif;
        inView = inView * SelectedDif;
        

        enemy = new Enemy(health, speed, agent, inRange, inView);
        agent = gameObject.GetComponent<NavMeshAgent>();
        self = this.gameObject;

        anim = this.gameObject.GetComponent<Animator>(); //important
    }

    public void Update()
    {
        Invoke("LookForPlayer", 5.0f);
       
        if (PlayerPos.FindPlayerPos(inRange, playerPos, inView, self))
        {
           TimeToFight.ChoseChasingWhatStateToAttackPlayer(agent, playerPos, self, anim, MultiAttack);           
        }
        
        if (health < 0.1f)
        {
            StartCoroutine(JustDies());            
        }
    }    
        
    IEnumerator JustDies() //important
    {
        //anim.SetBool("isDead", true);
        GetComponent<Animator>().SetBool("isDead", true);

        yield return new WaitForSeconds(4.5f);
        Destroy(this.gameObject);
    }

    public GameObject LookForPlayer()
    {
        playerPos = GameObject.FindGameObjectWithTag("Player");
        return playerPos;
    }

    private void OnTriggerEnter(Collider col)
    {
        if (col.gameObject.tag == "Weapon")
        {
            weaponGettingHitBy = new Weapon(col.gameObject, col.gameObject.GetComponent<WeaponBaseScript>().damagedealing);
            health = TakeDamage.TakeDmg(health, weaponGettingHitBy);
        }
    }
}
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Presumably, if both don't work, it means they're not the same thing? \$\endgroup\$
    – Vaillancourt
    Nov 23 at 23:04
  • 1
    \$\begingroup\$ Could you please reduce the code you've posted to an MCVE so that folks here can try and reproduce the problem on their end? \$\endgroup\$
    – Vaillancourt
    Nov 24 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.