Sin*_*rge 0 c# game-development unity-game-engine
我正在创建一个游戏,我想在玩家死亡时显示一个面板
我尝试了不同的方法,但似乎没有一个能做我想做的
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
要检查对象是否已被销毁,您应该OnDestroy像这样使用 MonoBehavior :
// Attach this script to the player object
public class DeadOrAlive : MonoBehaviour
{
public GameObject deadPanel;
void OnDestroy()
{
deadPanel.SetActive(true);
}
}
Run Code Online (Sandbox Code Playgroud)
您也可以不销毁玩家对象,而是将其设置为活动/非活动状态,但是要以这种方式检查玩家是死是活,您将需要一个单独的对象来检查活动状态:
//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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
有一段时间没有使用 unity 了,忘记了它会变得多么奇怪。
| 归档时间: |
|
| 查看次数: |
2550 次 |
| 最近记录: |