我对 Unity 很陌生,对于所有擅长 C# 的人来说,这可能看起来是一个愚蠢的问题,但我不知道为什么 OnTriggerEnter 在这个程序中不起作用。我已经按照我正在遵循的教程中的说明输入了它,但它在游戏中没有任何效果。帮忙吗?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DetectCollisions : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
void OnTriggerEnter(Collider other)
{
Destroy(gameObject);
Destroy(other.gameObject);
}
}
}
我还没有尝试过任何东西,我不知道该尝试什么。
好吧,你似乎又很难理解这里发生的事情了。
在
void Update()
{
// I am a local function within the Update method!
void OnTriggerEnter(Collider other)
{
Destroy(gameObject);
Destroy(other.gameObject);
}
}
您将
OnTriggerEnter
嵌套在 Update
方法下作为 本地函数。这样,当尝试通过其消息系统调用它时,Unity 就不会“知道”/找到它。
您宁愿将其作为类级别的普通方法
void Update()
{
}
void OnTriggerEnter(Collider other)
{
Destroy(gameObject);
Destroy(other.gameObject);
}
现在由于
Update
和 Start
是空的,实际上最好将它们全部删除;)
以防万一记录的方法不起作用,请进入
Edit
-> Project Settings
-> Physics
,并确保 Simulation Mode
设置为 Fixed Update
。
默认情况下,该设置设置为该值,但可能已更改,这将禁用触发碰撞。