Search Unity

NullReferenceException when creating a script which counts the jumps in screen

Discussion in 'Getting Started' started by romanpinkel, Sep 12, 2019.

  1. romanpinkel

    romanpinkel

    Joined:
    Sep 4, 2019
    Posts:
    20
    Im always getting this Nullreference:

    NullReferenceException: Object reference not set to an instance of an object
    JumpScript.Update () (at Assets/Scripts/Roman/JumpScript.cs:13)

    I tried to change some tutorials which counted points by players position.

    the code is really short:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI; //jump-counter
    3.  
    4. public class JumpScript : MonoBehaviour
    5. {
    6.     public static int spruenge = 0;
    7.     public Text sprungText;
    8.  
    9.  
    10.     // Update is called once per frame
    11.     public void Update()
    12.     {
    13.         sprungText.text = "Jumps: " + spruenge.ToString();
    14.         if (Input.GetKeyDown(KeyCode.Space))
    15.         {
    16.             spruenge++;          
    17.             Debug.Log("Spruenge: " + spruenge);        
    18.         }      
    19.     }  
    20. }
    21.  
    Please tell me where no reference is...

    I tried to move the "sprungText.text = "Jumps: " + spruenge.ToString();" into the if statement but then I cant apply the text in Unity...
     
    Last edited: Sep 12, 2019
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,192
    Well you haven't pasted the entire script, so we can't easily see where line 13 is. You can though, so you should just look at that line; that's why the error message tells you it.

    My guess though is that the error is line 10 of what you pasted above, and it's because you haven't assigned a Text object to the sprungText property.
     
  3. romanpinkel

    romanpinkel

    Joined:
    Sep 4, 2019
    Posts:
    20
    I corrected it and you're right about the line...
    where do I have to assign it?

    All assignment I knew was here and its there...
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,192
    You're right, it's assigned there. So probably you have another instance of your Jump Script somewhere else, that you're not thinking about right now, and that's where the problem is.

    Try inserting this before line 13:
    Code (csharp):
    1.  
    2.    if (sprungText == null) Debug.LogError("Achtung! sprungText == null", gameObject);
    3.  
    That second parameter, gameObject to Debug.LogError means that when you click on this error in the Console, it should highlight that object in your Hierarchy. This should help you pin down where it's gone wrong.
     
    Last edited: Sep 13, 2019
  5. romanpinkel

    romanpinkel

    Joined:
    Sep 4, 2019
    Posts:
    20
    you were correct, I was using the script 2 times but just one got the text script in there.
    After adding it to the other one, there are no more error messages :)

    Thanks!

    [Problem solved]
     
    JoeStrout likes this.