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

NullReferenceException error

Discussion in 'Scripting' started by Genesis4545, Jan 31, 2017.

  1. Genesis4545

    Genesis4545

    Joined:
    Jan 31, 2017
    Posts:
    7
    Hello everyone.

    I am currently working on my first Unity project with a small group. I am trying to trigger a Game Over screen when the player dies, but I'm having some trouble accessing a variable from another script. Here is the PlayerDeath script that I'm having trouble with:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerDeath : MonoBehaviour {
    6.  
    7.     public Transform Canvas;
    8.  
    9.  
    10.     void Update ()
    11.     {
    12.         GameObject Player = GameObject.Find("Ikari");
    13.         PlayerScript playerScript = Player.GetComponent<PlayerScript>();
    14.  
    15.         if (playerScript.isDead == true)
    16.         {
    17.             Canvas.gameObject.SetActive(true);
    18.             Time.timeScale = 0;
    19.         }
    20.     }
    21. }
    And the error code says "NullReferenceException: Object reference not set to an instance of an object. PlayerDeath.Update () (at Assets/Scripts/PlayerDeath.cs:13).
    Which to my undertsanding means it can't find something on line 13, however I have no idea why =/ As I said it's my first project so if anyone can educate me as to what I'm doing wrong I'd be ever so grateful.

    Thanks! :)
     
  2. Dennis59

    Dennis59

    Joined:
    Jan 8, 2013
    Posts:
    66
    The NullReferenceException is telling you that one of your variables is undefined. For line 13 that could be a couple of things.

    1. The variable "Player" is undefined. This would suggest that the call to Find did not find a GameObject called "Ikari". Check spelling etc.

    2. The variable "playerScript" could be undefined. This would suggest that the GameObject called Ikari does not have a PlayerScript component attached to it. Or, the PlayerScript component is attached to a child GameObject on Ikari. Assuming the latter use GetComponentInChildren<PlayerScript>().

    Hope that helps
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Using GameObject.find and getcomponent like that in update isn't very efficient. Since this is a death check, why not just trigger a gameover once the player dies? You are probably doing something either with a health or something where you can just determine if a player is dead instead of checking every frame like this.

    @Dennis59 beat me to it. :p Good chance that player is null.
     
  4. Dennis59

    Dennis59

    Joined:
    Jan 8, 2013
    Posts:
    66
    @Brathnann, good catch, I didn't even notice that. Call Find or GetComponent in Start, Awake or in a dedicated initialization method. And generally stay away from Find if you can. Very slow.
     
  5. Genesis4545

    Genesis4545

    Joined:
    Jan 31, 2017
    Posts:
    7
    The player was indeed null. I'd forgotten to remove Destroy(gameObject) from the player script >.> Thank you for your suggestions! ^^