How can I fix this error?

Hey,

I´m new to Unity and I´m making a 2D Jump & Run and I try to add lives in the LevelManager, but I always get an error:
NullReferenceException: Object reference not set to an instance of an object
LevelManager.Start () (at Assets/Scripts/LevelManager.cs:20)

That´s my script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LevelManager : MonoBehaviour
{
    public GameObject currentCheckpoint;
    //Player GameObject
    //aktueller Checkpoint
    public GameObject player;
    //Lebensanzeige
    public int lives;
    public Text livesText;
    private void Start()
    {
        LivesText.text = "Lives: " + lives.ToString();
    }
    private void Update() {
        if (Input.GetKeyDown(KeyCode.Escape)) {
            Debug.Log("Game ends!");
            Application.Quit();
        }
  }

when i remove this: livesText.text = lives.ToString(); and LivesText.text = "lives: " + lives.ToString(); the game works but the lives don´t do anything.

I pulled it into the LevelManager in the Inspector but it still gives me this error.

Please help me :).

Greeting from Austria

I think you just have a typo.
your definition is : public Text livesText;
in code : LivesText.text = …
→ change that to livesText.text = …

Hope it helps.

1 Like

You do have a typo, and that may fix it. But if you are getting a object not set to a instance error, that would be a different type of error.

Make sure you have dragged something into your livesText in the inspector as well (if @sylon suggestion doesn’t fix it)

Hey, thanks for the help but it didn´t work I already checked it and changed some things. Here is the new Script, it still don´t works :(. I checked the inspector and in Live Text is LiveText(Text) but it just won´t work.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class LevelManager : MonoBehaviour
{

    public GameObject currentCheckpoint;
    //Player GameObject
    //aktueller Checkpoint
    public GameObject player;
    //Lebensanzeige
    public int live;
    public Text LiveText;

    public void Start()
    {

        LiveText.text = "lives: " + live.ToString();
    }

    private void Update() {

        if (Input.GetKeyDown(KeyCode.Escape)) {
            Debug.Log("Game ends!");
            Application.Quit();
        }
  }  


    public void RespawnPlayer()
    {
        // Leben abziehen
        live = live - 1;
        //Lebensanzeige aktualisieren
        LiveText.text = "lives: " + live.ToString();
       
        //Überprüfen ob Spieler noch Leben hat
        if (live > 0)
        {
            //falls ja -> Respawn
            player.transform.position = currentCheckpoint.transform.position;
        }
        else
        {
            Time.timeScale = 0.0f;
            Debug.Log("Game over!");
        }

       
        //falls nein -> Spielende


        // Spieler an die Postition des Checkpoints bringen.
       
    }
}

Just saying it doesn’t work, doesn’t help. Are you getting a new error? What is happening? Or not happening?

This still not working?

It´s still the same Error: NullReferenceException: Object reference not set to an instance of an object
LevelManager.Start () (at Assets/Scripts/LevelManager.cs:20)

And you’re sure it’s set in the inspector?
If you add, just before the error line:

 if (LiveText == null) print("Live Text is null here.");

Does that print out in the console?

I’m having a vague recollection of a potential issue if all of the above is true. That is, did you change the type of variable that LiveText was, at any point? If so (or even if not, but for good measure). Select it in the inspector, clear it (set it to nothing for the variable), then drag it back in from the hiearchy. :slight_smile:

Nope :confused:

So, did you look at my latest post? Hard to tell b/c you quoted an earlier post…

I did that but it stil gives me the Error, so I don´t know what´s wrong. :frowning:
It says Live Text is null here in Console, but the Error is still there.

 public GameObject currentCheckpoint;
    //Player GameObject
    //aktueller Checkpoint
    public GameObject player;
    //Lebensanzeige
    public int live;
    public Text liveText;

      void Start()

    {
        if (liveText == null) print("Live Text is null here.");
        liveText.text = live.ToString();
              
    }

    private void Update() {

        if (Input.GetKeyDown(KeyCode.Escape)) {
            Debug.Log("Game ends!");
            Application.Quit();
        }
  }  


    public void RespawnPlayer()
    {

        // Leben abziehen
        live = live - 1;
        // Lebensanzeige aktualisieren
       liveText.text = live.ToString();
        // Überprüfen ob Spieler noch Leben hat
        if (live > 0)
        {
            // falls ja -> Respawn
            // spieler an die position des checkpoints bringen
            player.transform.position = currentCheckpoint.transform.position;
        }
        else
        {
            // falls nein -> Spielende!
            Time.timeScale = 0.0f;
            Debug.Log("Game over");
        }

    }
}

but the error is still there… hm… yes, well if it’s null that’s the problem :wink:
Did you double check the inspector & try remove the reference in the inspector & then re-assigning it from the hierarchy?

What do you mean null is the problem?
Yes I did that like 10x and it just won´t work, fucking frustrating :/.

http://imgur.com/a/lBjrr
Thats the inspector.

Are you certain you don’t have this component twice in your scene? One of which doesn’t have livesText assigned.

The problem is you have a null value for livesText, which means it is not assigned.

As @DanielQuick suggested, you may have another copy of the script in your scene. Use the search feature of the hierarchy to search for your LevelManager script to ensure you only see one copy in the scene.

I can´t find a second one and its assigned if that means that its like in the screenshot.

http://imgur.com/a/OKM9H

I can´t find a second one :(.

http://imgur.com/a/OKM9H

Sorry, not sure what to say. If you feel like attaching your project (which looks super small), I’d look at it whenever I’m free. :slight_smile:
You tried everything, including the second script possibility which was good to check… after that, no idea what could be wrong, sorry.

1 Like

Agreed, I’d have to look at the project myself as well. The only other thing I can think of is another script is doing something somewhere that is either destroying the LevelManager script, thus breaking your references. Or, something else is setting the text variable to null.

The third possibility is a simple unity glitch, but generally I don’t experience those on something as simple as this.

1 Like

Well thanks for the help :). I made something work now, a little bit ^^ if I go to my Charakter and give him Level Manager assigne the liveText and then delete the levelManager script out of my charakter then I can start the game without error, but I still don´t lose lives when i fall down or hit an enemy.

I would just make a new script if I would knew how but I´m new so its hard :smile:. I have this small heart icon on my screen and i want to lose 1 live when i die, but I can´t find a tutorial for that :/.