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();
}
}
public void RespawnPlayer()
{
// Leben abziehen
lives = lives - 1;
//Lebensanzeige aktualisieren
LivesText.text = "Lives: " + lives.ToString();
//Überprüfen ob Spieler noch Leben hat
if(lives > 0)
{
player.transform.position = currentCheckpoint.transform.position;
}
else
{
Time.timeScale = 0.0f;
Debug.Log("Game over!");
}
//falls ja -> Respawn
//falls nein -> Spielende
// Spieler an die Postition des Checkpoints bringen.
}
}
when i remove this: livesText.text = lives.ToString(); and LivesText.text = "lives: " + lives.ToString(); the game works.
I pulled it into the LevelManager in the Inspector but it sollt gives me this error.
Please help me :).
Greeting from Austria