【发布时间】:2020-10-12 03:14:47
【问题描述】:
对于我在 Unity 游戏中的健康系统,我有一个脚本负责我在游戏中的“敌人”生命值。游戏运行得很好,但脚本似乎没有做任何事情。我没有收到任何错误消息,但事实上它不起作用并且 Debug.Log 语句没有在控制台中弹出似乎是函数没有被正确调用或者其他东西出了问题。这是我的脚本:
using System.Diagnostics;
using UnityEngine;
public class Health : MonoBehaviour {
private float hitPoints = 5;
// Health popup
void announceUp()
{
UnityEngine.Debug.Log("If this message shows in Debug.Log, the script should be working.");
}
// Update is called once per frame
void Update()
{
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Bullet")
{
UnityEngine.Debug.Log("The enemy has been hit!");
hitPoints = hitPoints - 1f;
if (hitPoints == 0f)
{
UnityEngine.Debug.Log("The enemy has been eliminated!");
Destroy(gameObject);
}
}
}
}
}
我已经在互联网上四处寻找问题所在,但我找不到任何东西。有人可以告诉我我的编程有什么问题吗?
【问题讨论】:
-
把
OnTriggerEnter函数从你的更新方法中去掉。 -
这给了我一个错误,说缺少一个右大括号,即使它在那里......