Search Unity

Newbie having problems - "Object reference not set to an instance of an object"

Discussion in 'Editor & General Support' started by quillbolt, Apr 8, 2019.

  1. quillbolt

    quillbolt

    Joined:
    Apr 23, 2017
    Posts:
    2
    Hi. I've just started out using unity in the hopes of making a small hobby game. I have very little experience in coding apart from a basic understanding, nor am I experienced in the unity engine.

    I've been following along with Coding With Ejay's "Make a 2D RPG" series of tutorials. But I've hit an issue that I can't seem to solve myself, and I can't find any discrepancies in the code between what he has and I have.

    ----------

    My FollowPlayerX script, intended to make the attached enemy follow the player on the battle screen (as well as animate), has suddenly stopped working. This appears to be with the introduction of the player health and enemy attack system. This was [Episode #11] on Ejay's tutorials. The enemy character now doesn't move or animate.

    From what I understand it looks like the problem might lie in animator? But the bools are in the animator and its all connected properly. I can't figure out what else the problem could be, but it might just be a mistake in the code I missed. No clue.


    ----------


    The errors are as follows:

    NullReferenceException: Object reference not set to an instance of an object
    FollowPlayerX.Start () (at Assets/Scripts/Enemy/Enemy Battle/FollowPlayerX.cs:17)


    NullReferenceException: Object reference not set to an instance of an object
    FollowPlayerX.Update () (at Assets/Scripts/Enemy/Enemy Battle/FollowPlayerX.cs:29)


    My Script is here:

    Code (CSharp):
    1.     using System.Collections;
    2.     using System.Collections.Generic;
    3.     using UnityEngine;
    4.  
    5.     public class FollowPlayerX : MonoBehaviour
    6.     {
    7.  
    8.         public float speed;
    9.         public float stopDistance;
    10.         private SpriteRenderer render;
    11.         private Animator anim;
    12.  
    13.         public Transform target;
    14.  
    15.         void Start ()
    16.         {
    17.             Debug.Log(GameController.control.PlayerHP);
    18.             target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
    19.             render = GetComponent<SpriteRenderer>();
    20.             anim = GetComponent<Animator>();
    21.         }
    22.  
    23.         // Update is called once per frame
    24.         void Update ()
    25.         {
    26.             if (Vector2.Distance(transform.position, target.position) > stopDistance)
    27.             {
    28.                 StopCoroutine("DamagePlayer");
    29.                 anim.SetBool("Run", true);
    30.                 anim.SetBool("Idle", false);
    31.                 transform.position = Vector2.MoveTowards(transform.position, new Vector2(target.position.x, transform.position.y), speed * Time.deltaTime);
    32.             }
    33.  
    34.             else if (Vector2.Distance(transform.position, target.position) <= stopDistance)
    35.             {
    36.                 anim.SetBool("Run", false);
    37.                 anim.SetBool("Idle", true);
    38.             }
    39.  
    40.             if (target.position.x > transform.position.x)
    41.             {
    42.                 render.flipX = true;
    43.             }
    44.  
    45.             else if (target.position.x < transform.position.x)
    46.             {
    47.                 render.flipX = false;
    48.             }
    49.         }
    50.  
    51.         IEnumerator DamagePlayer()
    52.         {
    53.             for (int i = 0; i < GameController.control.PlayerHP;)
    54.             {
    55.                 GameController.control.PlayerHP = GameController.control.PlayerHP - GameController.control.EnemyDamageAmount;
    56.                 Debug.Log(GameController.control.PlayerHP);
    57.                 yield return new WaitForSeconds(GameController.control.EnemyReloadTime);
    58.             }
    59.         }
    60.  
    61.         void OnTriggerEnter2D(Collider2D col)
    62.         {
    63.             if (col.gameObject.name == "Hitbox")
    64.             {
    65.                 StartCoroutine("DamagePlayer");
    66.             }
    67.         }
    68.     }
    69.  
    70.  
    71.  


    If there's any additional information that would be useful in identifying the problem I'm happy to provide it. I just felt that it would be a bit overkill to upload the whole project.

    Also, ill be honest I wasn't sure if this was the right place to post this. I tried the "Unity Answers" forum but my post wouldnt go through, despite repeated attempts. If this is the wrong place, sorry!

    Cheers.
     
  2. TimmyTheTerrible

    TimmyTheTerrible

    Joined:
    Feb 18, 2017
    Posts:
    186
    Object reference not set to instance of an object is almost always because a get component call fails, or something wasn't linked to a property in the inspector.

    To avoid headaches, every time you make a GetComponent call, you should test to see if it returned null, before trying to use the component. Use Debug.Log() with reckless abandon while coding. As an example :
    Code (CSharp):
    1.  
    2. public class Example
    3. {
    4.     Animator anim;
    5.  
    6.     void Start()
    7.     {
    8.         animator = GetComponent<Animator>();
    9.          if(animator == null)
    10.             Debug.Log(name + " : No animator component found!");
    11.     }
    12. }
     
    quillbolt likes this.