- Home /
Why does this work but this doesn't even though they are the same thing?
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().SetBool("isDead", true); it works. I initialized anim by doing this: private Animator anim; and in Start anim = this.gameObject.GetComponent();. 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);
}
}
}
null error basically means that it didn't find the Animator component at the Start. Are you perhaps adding it later? Try make it public and set the reference manually in the inspector.
Your answer
Follow this Question
Related Questions
Root Movement with Additive Layers 0 Answers
How do I activate an animation when a gameobject enter the Collider of the other animated object 2 Answers
How to fix multiple character animations playing at the same time during a run animation? 1 Answer
3d Models, How to turn invisible 3 Answers
foreach fills array with 1 Answer