我正在创建一个游戏,我想在玩家死后显示一个面板
我尝试过不同的方法,但似乎没有一种能达到我想要的
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);
}
}
}
要检查对象是否已被销毁,您应该使用MonoBehavior的OnDestroy
,如下所示:
// Attach this script to the player object
public class DeadOrAlive : MonoBehaviour
{
public GameObject deadPanel;
void OnDestroy()
{
deadPanel.SetActive(true);
}
}
你也可以不破坏玩家对象,而是将其设置为活动/非活动,但要通过这种方式检查玩家是死是活,你需要一个单独的对象来检查活动状态:
//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);
}
}
}
已经有一段时间没有使用团结了,忘记了它会变得多么奇怪。
感谢@VincentBree,我就是这么做的
void Update()
{
if (!Player.activeSelf)
{
deadPanel.SetActive(true);
}
}