【发布时间】:2021-05-11 14:55:09
【问题描述】:
我正在尝试编写一个脚本来控制检查点的重生系统。在 PlayerController 脚本中,我将“健康”声明为 public int。然后,我在这个脚本中引用了“健康”。因此,如果玩家死亡(生命值 = 0),它将在检查点重生。
但是,当我在此脚本中提及“健康”时,我必须将“int”转换为“unityengine.gameobject”。谁能帮帮我。我刚开始学习 C#。
public class PlayerPros : MonoBehaviour
{
private savePoint sp;
private GameObject health;
private void Start()
{
health = GetComponent<GameObject>();
sp = GameObject.FindGameObjectWithTag("SP").GetComponent<savePoint>();
transform.position = sp.lastCheckPointPos;
}
private void Update()
{
if ( health = 0) //error occurs here
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
}
【问题讨论】:
-
health = 0表示您将值分配给变量。您可能的意思是等于:health == 0 -
GameObject也不是组件 .. 你的意思是gameObject为你提供GameObject这个脚本附加到的属性吗?好吧,错误应该很容易解释..GameObject不是int...您是否尝试通过GetComponent访问不同的组件?
标签: c# unity3d type-conversion