Unity UI Text enable and disable by a C# Script?

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

  1. public Text GameOverText;

  2. if (GameOverText.enabled==true)
    {
    GameOverText.enabled = false;
    }

  3. GameOverText.enabled = true;

I attached this Script to my Player and in the Inspector i draw the “GameOverText” in the Field Text

alt text

But it doesnt Work, i dont know why :frowning:

Error: NullReferenceException: Object reference not set to an instance of an object
CharakterHealth.Start () (at Assets/Scripts/CharakterHealth.cs:19)

Thanks for help :slight_smile:

@fakemaster So you could try this
GameOverText .gameObject.SetActive(false);
and if you need to turn it on
GameOverText .gameObject.SetActive(true);

Hope this helps. Let me know if that works
also make sure you drag your text in you script from the editor

unfortunately it doesnt work, same error as before :confused: