本文介绍了团结一致的时间后销毁特定的游戏对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我现在还停留在脚本中.当一个带有我的脚本的游戏对象发生带有特定游戏对象的触发事件时,我想在一段时间后销毁该特定游戏对象.
I kinda stuck with my script right now. When a gameObject, with my script attached to it, has a trigger event with a specific gameObject, I want to destroy the specific gameObject after an amount of time.
所以我来到这里:
void OnTriggerEnter ( Collider other) {
if (other.gameObject.tag == "leaf1"){
StartCoroutine (LeafDestruction ());
}
}
IEnumerator LeafDestruction(){
yield return new WaitForSeconds (5);
Destroy (gameObject);
}
我知道这是一个菜鸟错误,但是我想我错过了一些东西,因为当我运行此脚本时,它会破坏附带有脚本的gameObject,而不是特定的gameObject(带有标签).
I know it's a noob mistake but i think i miss something, because when i run this script, it destroys the gameObject with the script attached to it, and not the specific gameObject(with tags).
我该如何解决?
推荐答案
一个简单的解决方案是使用销毁函数:
A simpler solution is to use the 2nd parameter of the Destroy function :
if (other.gameObject.tag == "leaf1")
Destroy( other.gameObject, 5.0f ) ;
这篇关于团结一致的时间后销毁特定的游戏对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!