Problem is not reproducible or outdated
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
- 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 :)
the code
GameOverText.SetActive(false);
should work. However, it does not also work for me?
Answer by SteveDizzle · Mar 11, 2017 at 10:56 PM
@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
Answer by Fakemaster · Mar 12, 2017 at 09:31 AM
unfortunately it doesnt work, same error as before :/
Ins$$anonymous$$d of using
 if (GameOverText.enabled==true)
you could try
 (GameOverText.isActiveAndEnabled == true)
 {
 GameOverText.gameObject.SetActive(false);
 }
I hope that helps!
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                