Sooo, at first sry for my not so good english^^.
In my Game, i want to disable the Text “Game Over” at the start of the Game and enable it if the Lives of the Player are <= 0.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CharakterHealth : MonoBehaviour {
public float MomentanLeben { get; set; }
public float MaxLeben { get; set; }
public Slider Lebensanzeige;
public Text GameOverText;
void Start ()
{
MaxLeben = 100f;
MomentanLeben = MaxLeben;
Lebensanzeige.value = Lebensrechner();
GameOverText = GetComponent<Text>();
if (GameOverText.enabled==true)
{
GameOverText.enabled = false;
}
}
void Update ()
{
if (Input.GetMouseButtonDown(0))
Schaden(5f);
}
void Schaden(float Schadenwert)
{
MomentanLeben -= Schadenwert;
Lebensanzeige.value = Lebensrechner();
if (MomentanLeben <= 0)
{
Die();
}
}
float Lebensrechner()
{
return MomentanLeben / MaxLeben;
}
void Die()
{
MomentanLeben = 0;
GameOverText.enabled = true;
}
}
This is a Script with a Slider to see Player´s lives and if he is dead, the Text “Game Over” should appear.
The Lines i used to do it are
-
public Text GameOverText;
-
if (GameOverText.enabled==true)
{
GameOverText.enabled = false;
} -
GameOverText.enabled = true;
I attached this Script to my Player and in the Inspector i draw the “GameOverText” in the Field Text
But it doesnt Work, i dont know why
Error: NullReferenceException: Object reference not set to an instance of an object
CharakterHealth.Start () (at Assets/Scripts/CharakterHealth.cs:19)
Thanks for help