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. Dismiss Notice

Object reference not set to an instance of an Object in C#

Discussion in 'Scripting' started by AlexB05, Mar 5, 2019.

  1. AlexB05

    AlexB05

    Joined:
    Mar 5, 2019
    Posts:
    2
    [SOLVED] Hey everyone. I am having an error in one of my scripts called "object reference not set to an instance of an object". Usually, I can debug a script but I never understood this error. Can someone help me?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerDamage : MonoBehaviour
    6. {
    7.     EnemyHealth enemyHealth;
    8.     public GameObject Enemy;
    9.     public int DPS = 20;
    10.     public int TimeBetweenShots = 1;
    11.     //private GameObject Player;
    12.     float timer;
    13.  
    14.     void Start()
    15.     {
    16.         GameObject Enemy = GameObject.Find("Enemy");
    17.         EnemyHealth enemyHealth = Enemy.GetComponent<EnemyHealth>();
    18.     }
    19.  
    20.     public void OnCollisionEnter(Collision collision)
    21.     {
    22.         if (collision.gameObject.name == "Enemy")
    23.         {
    24.             enemyHealth.MaxHP -= DPS;
    25.         }
    26.     }
    27.  
    28.  
    29.     //void Attack(int amount)
    30.     //{
    31.      //   Timer = 0f;
    32.      //   Timer += Time.deltaTime;
    33.      //   if (enemyHealth != null)
    34.      //       CurrentHP -= amount;
    35.    // }
    36.  
    37.  
    38. }
    39.  
    Also, here is my EnemyHealth script that I made
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyHealth : MonoBehaviour
    6. {
    7.     public int MaxHP = 100;
    8.     public int CurrentHP;
    9.     EnemyFollow enemyFollow;
    10.     bool damaged;
    11.     bool isDead;
    12.  
    13.     void Awake()
    14.     {
    15.         enemyFollow = GetComponent<EnemyFollow>();
    16.         CurrentHP = MaxHP;
    17.     }
    18.  
    19.     public void TakeDamage(int amount)
    20.     {
    21.         damaged = true;
    22.         CurrentHP -= amount;
    23.         if (CurrentHP <= 0 && !isDead)
    24.         {
    25.             Death();
    26.         }  
    27.     }
    28.  
    29.     void Death()
    30.     {
    31.         isDead = true;
    32.         enemyFollow.enabled = false;
    33.     }
    34. }
     
    Last edited: Mar 6, 2019
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    That error means that you are trying to use a variable with a null value. What line does the error occur on?
     
  3. AlexB05

    AlexB05

    Joined:
    Mar 5, 2019
    Posts:
    2
    I actually just fixed the problem. I forgot on PlayerDamage script to make EnemyHealth public but still thank you!
     
  4. malcomjarr

    malcomjarr

    Joined:
    Nov 18, 2020
    Posts:
    7
    An Object is an instance of a Class , it is stored some where in memory. A reference is what is used to describe the pointer to the memory location where the Object resides. The message "object reference not set to an instance of an object" (NullReferenceException) means that you are referring to an object the does not exist or was deleted or cleaned up. In order to prevent the error, objects that could be null should be tested for null before being used.

    Code (CSharp):
    1. if (mClass != null)
    2. {
    3.   // Go ahead and use mClass
    4.   mClass.property = ...
    5. }
    6. else
    7. {
    8.   // Attempting to use mClass here will result in NullReferenceException
    9. }
    A NullReferenceException typically reflects developer error and is thrown in the following scenarios:

    • Forgotten to instantiate a reference type.
    • Forgotten to dimension an array before initializing it.
    • Is thrown by a method that is passed null.
    • Get a null return value from a method, then call a method on the returned type.
    • Using an expression to retrieve a value and, although checking whether the value is null.
    • Enumerating the elements of an array that contains reference types, and attempt to process one of the elements.
     
  5. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    @malcomjarr while I think it's kind of you to respond, I recommend you check the date and state of a post you respond to. You are almost two years late to the party, and the OP clearly stated that the issue was solved.

    Now, it's still great that you want to help, so here's to you helping on a more recent thread :)