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

Beginner - NullReferenceException quick help needed

Discussion in 'Scripting' started by NeroAce, Mar 30, 2016.

  1. NeroAce

    NeroAce

    Joined:
    Mar 30, 2016
    Posts:
    3
    So what I am trying to do is as follows: When the player touches the "Key" the Canvas object should be set to active. My problem is that I am using two scripts to achieve this. Here's what I have in the scripts:

    Player.cs:
    Code (CSharp):
    1. private GameManager GameController;
    2.  
    3. private void OnTriggerEnter(Collider other)
    4.     {
    5.         if (other.gameObject.CompareTag("Key")) {
    6.             other.gameObject.SetActive (false);
    7.             GameController.GameOver ();
    8.         }
    9.     }

    and

    GameManager.cs:
    Code (CSharp):
    1. public Canvas gameOverText;
    2.  
    3. private void Start () {
    4.         StartCoroutine(BeginGame());
    5.         gameOverText.gameObject.SetActive (false);
    6.     }
    7.  
    8. public void GameOver () {
    9.         gameOverText.gameObject.SetActive (true);
    10.     }
    but yea like I said I'm getting a NullReferenceException.

    I've tried about 30 different ways of getting it to work (self taught C# and unity coding) so please bear with me
     
  2. DevBrandanBK1998

    DevBrandanBK1998

    Joined:
    Aug 31, 2015
    Posts:
    2
    What is the start coroutine for? Could you also post what line your getting the error on?
     
  3. Austin-Gregory

    Austin-Gregory

    Joined:
    Sep 19, 2013
    Posts:
    78
    Okay, so you have GameController that is set to private and not assigned anything. The easiest thing for you to do to get going with it is going to be to make it public, and drop the GameObject that has your GameController on the field in the inspector.

    I'm not sure if the issue is with your gameOverText since it is public. You could have it wired up in the inspector, I just can't tell since you didn't post your full error.

    Couple notes, you have two fields using two different naming styles. Your best option would be to pick a naming convention and stick with it.

    And with the error, it helps more than anything to just read the error. Don't see the red and just assume something bad happened, the errors are very descriptive. In this case, it will tell you what file has the null reference and even give you the line number. Double clicking the error will take you to that file and that specific line. A null reference is just saying your trying to reference something that hasn't been assigned a value, therefore it defaults to null.

    This is extremely basic C# as well basic Unity workflow. I would suggest you go through some C# and Unity guides, they have some great ones in the Learn section.
     
  4. NeroAce

    NeroAce

    Joined:
    Mar 30, 2016
    Posts:
    3
    In the parts of the code I provided I get the error on line 7 of the Player.cs.
    The Coroutine starts the random map creation (which all works well so far). I hope that answers the question to an extent.



    I tried this, but it would neither accept the GameController Object (NullObject) nor the GameManager script, so I wasn't sure why or what was missing. I shall try it again, to make sure.

    Thanks again for the help
     
  5. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,381
    NullReferenceException means a variable you're trying to use is null. It can't be used because there is nothing there. You either have not assigned anything to the variable or have not initialized it.

    The error tells you exactly what line the error is on. Something on that exact line is null. You can use Debug.Log(itemInQuestion); to see if it returns null or not in the console.
     
  6. caseyboundless

    caseyboundless

    Joined:
    Oct 17, 2011
    Posts:
    590
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Test : MonoBehaviour {
    5.     private GameManager gameController; // always start with lower case letters and not numbers or capital letters
    6.  
    7.     void Awake()
    8.     {
    9.         gameController = GameObject.Find("Obj Where GameManager is attached").GetComponent<GameManager>(); // Find and get components from obj
    10.  
    11.     }
    12.  
    13.  
    14.     // Use this for initialization
    15.     void Start () {
    16.    
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update () {
    21.    
    22.     }
    23.  
    24.  
    25.     void OnTriggerEnter(Collider other)
    26.     {
    27.         if (other.gameObject.CompareTag("Key")) {
    28.             other.gameObject.SetActive (false);
    29.             gameController.GameOver ();
    30.         }
    31.     }
    32.  
    33.  
    34. }
    35.  
     
    NeroAce likes this.
  7. NeroAce

    NeroAce

    Joined:
    Mar 30, 2016
    Posts:
    3


    THANK YOU! Here is what I believe my problem was:
    Code (CSharp):
    1. private GameManager gameController;
    I thought that the gameController was the name of the object I was trying to reference to.

    Through that it makes sense finding the object after the game has been initiated
    Code (CSharp):
    1. void Awake () {
    2.         gameController = GameObject.Find ("playGame").GetComponent<GameManager> ();
    3.     }
    where playGame is the game controller that was previously called gameController (see the problem here?)

    afterwards it was indeed as simple as calling the GameOver function in gameController.

    I hope this might make sense to someone else that might be starting out, and again thanks a bunch to caseyboundless for the clean script helping me figure out my issues!
     
  8. caseyboundless

    caseyboundless

    Joined:
    Oct 17, 2011
    Posts:
    590
    Glad I could help.