Help with Enemy death Animation!
Hey I'm getting a NullReferenceException error and not sure why... Any help would be appreciated! Thanks!
Error line :
"anim.SetBool ("Dead", true);"
using UnityEngine; using System.Collections;
public class EnemyHelthManager : MonoBehaviour {
public int MaxHealth;
public int CurrentHealth;
public GameObject bloodpop;
private bool Dead;
private Animator anim;
// Use this for initialization
void Start () {
CurrentHealth = MaxHealth;
}
// Update is called once per frame
void Update () {
if (CurrentHealth <= 0)
{
Dead = true;
anim.SetBool ("Dead", true);
}
if (CurrentHealth < MaxHealth)
Instantiate (bloodpop, transform.position, transform.rotation);
}
public IEnumerator DeathAnim()
{
if (Dead == true)
{
yield return new WaitForSeconds (3);
Destroy (gameObject);
}
}
public void HurtEnemy(int damageToGive)
{
CurrentHealth -= damageToGive;
GetComponent<AudioSource>().Play();
}
public void SetMaxHealth ()
{
CurrentHealth = MaxHealth;
}
}
Answer by PredatorProductions · Jan 02, 2017 at 10:15 PM
So I figured it out (for the 300 people following the question).
What i did was put this in the start function:
Dead = false;
{
anim = GetComponent<Animator> ();
MyRigidbody = GetComponent<Rigidbody2D> ();
}
}
And I added this in the MonoBehaviour {
private Rigidbody2D MyRigidbody;
}
Basically i just told the script to find the Rigidbody, and use that to find the animator. At least thats how I understood it. Anyways it's animating now but its not destroying the GameObject after the IEnumerator waits for (1) second. So If i figure that out I'll post it or if anyone has any help with that let me know!
Thanks.
Your answer
Follow this Question
Related Questions
Boolean prevent script from working 0 Answers
Bool changes back instantly 3 Answers
Can anyone tell me what the issue is with my code? (C#) 1 Answer
The name animator does not exist in the current context... Help? 1 Answer
NullReferenceException: Object Reference not set to an instance of an object 0 Answers