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

Object reference not set to an instance of an object

Discussion in 'Scripting' started by Zammystuff, Apr 6, 2020.

  1. Zammystuff

    Zammystuff

    Joined:
    Jun 30, 2015
    Posts:
    9
    Hello everyone. I am an extremely new programmer and im following a Udemy course in development. I have added a spawn manager to the game, and ive followed to tutorial exactly, but im not sure exactly what this mean. i have done some research into the message but from (as far as i understand, and im sure im wrong) im referencing everything correctly.

    This message only happens after and instantiated enemy (Enemy Clone) collides with the player. any help would be appreciated.

    Debug Log Message:
    NullReferenceException: Object reference not set to an instance of an object
    Player.Damage () (at Assets/Script/Player.cs:94)
    Enemy.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Script/Enemy.cs:36)

    Enemy Script
    --------------------------------------------------------

    Code (CSharp):
    1. public class Enemy : MonoBehaviour
    2. {
    3.     private float x;
    4.     private float y;
    5.     private float z;
    6.     public float EnemySpeed = 4.0f;
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.         transform.position = new Vector3 (0, 7.0f, 0);
    11.        
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.          x = Random.Range (-8.79f, 8.79f);
    18.         transform.position += Vector3.down * EnemySpeed * Time.deltaTime;
    19.          if (transform.position.y <= -5.74f)
    20.     {
    21.         transform.position = new Vector3 (x, 7.0f, transform.position.z);
    22.    
    23.     }
    24.    
    25.     }
    26.     private void OnTriggerEnter(Collider other)
    27.     {  
    28.         Debug.Log("Hit: "+ other.transform.name);
    29.         if (other.gameObject.CompareTag("Player"))
    30.         {
    31.             Player player = other.transform.GetComponent<Player>();
    32.            
    33.            if (player != null)
    34.            {
    35.                player.Damage();
    36.            }
    37.            Destroy(this.gameObject);
    38.  
    39.         }
    40.        else if (other.gameObject.CompareTag("Laser"))
    41.         {
    42.             Destroy(this.gameObject);
    43.         }
    44.        }
    45.    
    46. }
    47.  
    Code (CSharp):
    1. public class Player : MonoBehaviour
    2. {
    3.     // Start is called before the first frame update
    4.     public float moveSpeed = 5.0f;
    5.     [SerializeField]
    6.     private GameObject Laser;
    7.     private GameObject LaserClone;
    8.     [SerializeField]
    9.     private float fireRate = 0.5f;
    10.     [SerializeField]
    11.     private float nextFire = 0.0f;
    12.     [SerializeField]
    13.     public int playerLives = 3;
    14.     [SerializeField]
    15.     private int currentLives = 3;
    16.     private SpawnManager _spawnManager;
    17.     void Start()
    18.     {
    19.         //get current position. move to origin
    20.         transform.position = new Vector3(0,0,0);
    21.         _spawnManager = GameObject.Find("Spawn Manager").GetComponent<SpawnManager>();
    22.         if(_spawnManager = null)
    23.         {
    24.             Debug.LogError("Spawn Manager is Null.");
    25.         }
    26.        
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     void Update()
    31.     {                   //Player controls
    32.         if (Input.GetKey(KeyCode.A))
    33.      transform.Translate(Vector3.left * moveSpeed * Time.deltaTime);
    34.  
    35.         if (Input.GetKey(KeyCode.D))
    36.         transform.Translate(Vector3.right* moveSpeed * Time.deltaTime);
    37.  
    38.         if (Input.GetKey(KeyCode.W))
    39.         transform.Translate(Vector3.up * moveSpeed * Time.deltaTime);
    40.  
    41.         if (Input.GetKey(KeyCode.S))
    42.         transform.Translate(Vector3.down * moveSpeed * Time.deltaTime);
    43.  
    44.         //if player position on y is more than 0
    45.         //y position = 0
    46.         //this controls player bounds
    47.  
    48.         if (transform.position.y >= 3.70f)
    49.         {
    50.             transform.position = new Vector3 (transform.position.x, 3.70f, transform.position.z);
    51.         }
    52.         else if(transform.position.y <= -4.39f)
    53.         {
    54.             transform.position = new Vector3 (transform.position.x, -4.38f, transform.position.z);
    55.         }
    56.         if(transform.position.x <= -8.36f)
    57.         {
    58.             transform.position = new Vector3 (9.36f, transform.position.y, transform.position.z);
    59.         }
    60.         else if (transform.position.x >= 10.0f)
    61.         {
    62.             transform.position = new Vector3 (-8.35f, transform.position.y, transform.position.z);
    63.         }
    64.          if (Input.GetKeyDown(KeyCode.Space) && Time.time > nextFire)
    65.        
    66.          {
    67.              Debug.Log("BAM!");
    68.               nextFire = Time.time + fireRate;
    69.               Vector3 offset = new Vector3(0,0.8f,0);
    70.               LaserClone = Instantiate(Laser, transform.position + offset, Quaternion.identity);
    71.               LaserClone.gameObject.tag = "Laser";
    72.               Destroy(LaserClone, 3.5f);
    73.            
    74.            
    75.          }
    76.        
    77.    
    78.  
    79.     }
    80.     public void Damage()
    81.     {
    82.         playerLives -= 1;
    83.  
    84.         //check if dead
    85.         //if true destroy us
    86.  
    87.         if(playerLives < 1)
    88.         {
    89.            
    90.             _spawnManager.OnPlayerDeath();
    91.             Destroy(this.gameObject);
    92.         }
    93.  
    94.     }
    95.      public void OnTriggerEnter(Collider other)
    96.      {
    97.          if(other.gameObject.CompareTag("Enemy"))
    98.          currentLives =- 1;
    99.          Debug.Log("Health= " + currentLives);
    100.      }
    101.  
    102.    
    103. }
    104.  
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,204
    _spawnManager is null when you do this:

    Code (csharp):
    1. _spawnManager.OnPlayerDeath();
    Since you're checking that it's not in Start, I'm guessing that it's gotten destroyed at some point.
     
  3. Zammystuff

    Zammystuff

    Joined:
    Jun 30, 2015
    Posts:
    9
    So as it turns out on my Null check in my player void Start
    i had used "=" instead of "==" can anyone explain why this would make such big difference.
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,204
    Oh, didn't spot that.

    = is assignment (x = y sets x to be y), while == is an equality check (x == y checks if x is y)
     
  5. Zammystuff

    Zammystuff

    Joined:
    Jun 30, 2015
    Posts:
    9
    Makes sense haha. Only took me long enough to wanna bang my head against my desk. Much appreciated bro.