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.
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.
}
}
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.
I did that but it stil gives me the Error, so I don´t know what´s wrong.
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
Did you double check the inspector & try remove the reference in the inspector & then re-assigning it from the hierarchy?
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.
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.
You tried everything, including the second script possibility which was good to check… after that, no idea what could be wrong, sorry.
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.
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 . 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 :/.