[英]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.