Home > database >  Unity: Even when the "player" game object gets destroyed, the text doesn't appear
Unity: Even when the "player" game object gets destroyed, the text doesn't appear

Time:02-19

I've been using Unity to create a simple 2D game but the problem is that even when the game object "player" gets destroyed, the gameobject "isDead" (text) doesn't appear.

This is my script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class youDied_Text : MonoBehaviour
{
    private Transform player;
    private Text isDead;
    // public static bool isDead;
    // Start is called before the first frame update
    

    private void Start() {
        isDead = GetComponent<Text>();    
    }
    void checkForDeath()
    {
        if (player==false)
        {
            isDead.gameObject.SetActive(true);
        }
        else
        {
            isDead.gameObject.SetActive(false);
        }
    }

    // Update is called once per frame
    void Update()
    {
        player = GameObject.FindWithTag("Player").transform;
        checkForDeath();
    }
}

This script is attached in the text which I need to display in UI element.

CodePudding user response:

  • As was noted currently you would get a NullReferenceException which is definitely not what you want.

  • There is absolutely no need / redundancy going through Transform at all actually. Simply store the GameObject reference instead

  • You are currently setting the object to inactive which has the Text attached ... which is the same object your component is attached to as well!

    => As soon as you end up in the second case once you set it to inactive => from now on Update is never called anymore!


In general as it sounds like this should only happen once anyway I would use a more event driven approach and have a component on your player like e.g.

public class Player : MonoBehaviour
{
    public UnityEvent onDied;

    private void OnDestroy ()
    {
        onDied.Invoke();
    }
}

And then simply attach a listener/callback to that event once without poll checking states. You can do this either via the Inspector directly (just like in e.g. Button.onClick) or via code like e.g.

public class youDied_Text : MonoBehaviour
{
    // Already reference things via the Inspector if possible!
    [SerializeField] private GameObject player;
    [SerializeField] private Text isDead;

    private void Awake() 
    {
        if(!isDead) isDead = GetComponent<Text>(); 

        isDead.gameObject.SetActive(false);

        // If you want to rather set it via the Inspector remove all the rest here
        //if(!player) player = GameObject.FindWithTag("Player"). GetComponent<Player>();
        // or even simpler
        if(!player) player = FindObjectOfType<Player>(); 
        player.onDied.AddListener(OnPlayerDied);  
    }

    // If you want to rather set it via the Inspector make this public
    private void OnPlayerDied()
    {
        isDead.gameObject.SetActive(true);
    }
}
  • Related