问题描述
我已经开始使用C#慢慢学习Unity,到目前为止,这是一个爆炸!
I've started learning Unity slowly using C# and it's been a blast thus far!
我遇到了一个小问题(我希望这是一个小问题)并陷入困境,从那以后一直在质疑我的理智.
I've run into a small problem (I hope it's a small problem) and gotten stuck, and have been somewhat questioning my sanity ever since.
在我首先运行的主脚本中,我有运行时生成原始图元(球体)并将其附加脚本的代码,脚本会检查该球体是否已触发.
In my main script which runs first I have code that generates a primitive (sphere) on the fly and attaches a script to it, the scrip checks to see if the sphere has been triggered.
主脚本
bool createNav (Vector3 _start) {
GameObject nav = GameObject.CreatePrimitive(PrimitiveType.Sphere);
nav.AddComponent<NavTrigger>();
nav.collider.isTrigger = true;
nav.transform.localScale = new Vector3(1f,1f,1f);
nav.transform.position = _start;
return nav.GetComponent<NavTrigger>().Triggered;
}
我的其他脚本
private bool triggered = false;
void OnTriggerEnter() {
this.triggered = true;
}
public bool Triggered {
get { return this.triggered; }
}
可悲的是,无论运行OnTriggerEnter代码如何,它仍然会返回false.
Sadly, it still returns false regardless of it running the OnTriggerEnter code.
如果有人有任何想法或建议,请告诉我,我将尽一切努力使这项工作成功.
Please, if anyone has any ideas or suggestions at all let me know I will try and do just about anything to make this work.
非常感谢您的帮助! :)
Thank you very much for your help! :)
推荐答案
OnTriggerEnter 需要一个参数(其他碰撞器),否则将与Unity期望的签名不匹配.
OnTriggerEnter requires a parameter (Collider other) or it won't match the signature Unity expects.
private bool triggered = false;
void OnTriggerEnter(Collider other) {
Debug.Log("Triggered");
this.triggered = true;
}
public bool Triggered {
get { return this.triggered; }
}
}
此外,如文档所述:
这篇关于调用了OnTriggerEnter,但从未设置变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!