卫生系统脚本未在Unity中运行;没有错误或弹出Debug.Log语句



对于我在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);
}
}
}
}
}

我在网上找了找问题,但什么也找不到。有人能告诉我我的程序可能出了什么问题吗?

您的scrpt当前不工作,因为您正在Update()方法中定义OnTriggerEnter()方法。当你这样做的时候,你定义了一个局部函数,当实际冲突发生时,Unity不能调用该函数。所以你的OnTriggerEnter()函数永远不会被调用。

示例:

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);
}
}
}
}

相关内容

最新更新