Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Object reference not set to an instance of an object for a Animation?

Discussion in 'Scripting' started by DanSingh, Jul 2, 2016.

  1. DanSingh

    DanSingh

    Joined:
    Feb 5, 2015
    Posts:
    98
    Greetings,
    Recently for a while I've been struggling with this problem. In the enemyhealth script the death method is suppose to play the death animation clip from a zombie that I got free from the Assets store however, it keeping giving me the "Object reference not set to an instance of an object" error when the function is called, and I can't figure out why please help me. I also have the animator controller attached to the zombie. Anything neccesary is attached. I've also attached my script where the zombie can attack, but I don't think this causes anything.

    Thanks
    Code (CSharp):
    1. //EnemyHealth Script
    2.  
    3. void Update ()
    4.     {
    5.         if(EnemHealth <= 0)
    6.             Death();
    7.            
    8.     }
    9.  
    10.     public void TakeDamage(float Dam)
    11.     {
    12.         EnemHealth -= Dam;
    13.     }
    14.  
    15.     void Death()
    16.     {
    17.        
    18.         navMesh.enabled = false;
    19.         anim.SetBool("isDead",true);
    20.         //coll.isTrigger = true;
    21.    
    22.         Destroy(gameObject,1.5f);
    23.     }
    Code (CSharp):
    1. //EnemyAttack Script
    2. void OnTriggerStay (Collider col)
    3.     {
    4.         timer += Time.deltaTime;
    5.  
    6.         if (col.gameObject.tag == "Player" && timer > timeBetweenAtks)
    7.         {
    8.             timer = 0.0f;
    9.             playHealth.TakeDamage (attackDam);
    10.             playHealth.takenDam = true;
    11.             anim.SetBool("isAttacking",true);
    12.         }
    13.     }
    14.  
    15.     void OnTriggerExit(Collider col)
    16.     {
    17.         if (col.gameObject.tag == "Player")
    18.         {
    19.             playHealth.takenDam = false;
    20.             anim.SetBool("isAttacking",false);
    21.         }
    22.     }
     

    Attached Files:

  2. DanSingh

    DanSingh

    Joined:
    Feb 5, 2015
    Posts:
    98
    NeverMind I fixed it, I'm so blind, I never set up my reference for the animator.