Question by
Steev98 · Jun 16, 2017 at 09:32 PM ·
gameobjectmaterialtextlevel
I always get the same Error and can´t fix it. Help!
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 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.
}
}
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 checked the inspector and in Live Text is LiveText(Text) but it just won´t work.
Help would be really nice :).
Comment