简体   繁体   English

有没有办法检查游戏对象是否已被销毁?

[英]Is there a way to check if a GameObject has been destroyed?

I'm creating a game and i want to show a panel when the player is dead我正在创建一个游戏,我想在玩家死亡时显示一个面板

I've tried different approaches but none seems to do what I want我尝试了不同的方法,但似乎都没有做我想做的事

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

public class DeadOrAlive : MonoBehaviour
{

    public GameObject Player;
    public GameObject deadPanel;

    void Update()
    {
        if (!GameObject.FindWithTag("Player"))
        {
            deadPanel.SetActive(true);
        }
    }


}

To check if a object has been destroyed, you should use MonoBehavior's OnDestroy like so:要检查 object 是否已被销毁,您应该像这样使用 MonoBehavior 的OnDestroy

// Attach this script to the player object
public class DeadOrAlive : MonoBehaviour
{
    public GameObject deadPanel;

    void OnDestroy()
    {
        deadPanel.SetActive(true);
    }
}

You can also instead of destroying the player object, set it to active/inactive, but to check if the player is dead or alive this way, you will need a separate object which checks the active state:您也可以不破坏播放器 object,而是将其设置为活动/非活动,但要检查播放器是死是活,您需要一个单独的 object 来检查活动的 Z9ED39E2EA931586B6A985A6942EF573

//Attach this to a object which isn't a child of the player, maybe a dummy object called "PlayerMonitor" which is always active
public class DeadOrAlive : MonoBehaviour
{
    public GameObject deadPanel;

    void Update()
    {
        if (!GameObject.FindWithTag("Player"))
        {
            deadPanel.SetActive(true);
        }
    }
}

Haven't used unity in a while and forgot how weird it could get.有一段时间没有使用统一,忘记了它会变得多么奇怪。

Thanks to @VincentBree this is how I did it感谢@VincentBree,我就是这样做的

  void Update()
{
    if (!Player.activeSelf)
    {
        deadPanel.SetActive(true);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Unity “GameObject”类型的 object 已被销毁 - Unity The object of type “GameObject” has been destroyed 当游戏对象被销毁时出现 MissingReferenceException - MissingReferenceException when gameObject has been destroyed 如何从已被破坏的外部GameObject执行协程 - How to execute Coroutine from external GameObject which has been destroyed “ GameObject”类型的对象已被破坏,但您仍在尝试访问它 - The object of type 'GameObject' has been destroyed but you are still trying to access it 如何检查播放器是否已被销毁 - how to check if player has been destroyed unity 'GameObject' 类型的 object 已被破坏,但您仍在尝试访问它 - unity The object of type 'GameObject' has been destroyed but you are still trying to access it 错误 MissingReferenceException:“GameObject”类型的 object 已被破坏,但您仍在尝试访问它 - Error MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it Brackeys 2d platformer GameObject'已被破坏,但您仍在尝试访问它 - Brackeys 2d platformer GameObject' has been destroyed but you are still trying to access it 如何修复 Unity 中的“'GameObject' 类型的对象已被销毁,但您仍在尝试访问它”错误? - How to fix "the object of type 'GameObject' has been destroyed, but you are still trying to access it" error in Unity? 如何修复“'GameObject' 类型的 object 已被破坏,但您仍在尝试访问它。” 统一 - How I can fix the “The object of type 'GameObject' has been destroyed but you are still trying to access it.” Unity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM