在UNITY JAVASCRIPT中销毁后实例化对象

时间:2014-02-04 14:36:24

标签: unity3d instantiation destroy unityscript

销毁命令:

var destroyTime : int; // This is the time in seconds
function Start(){
    yield WaitForSeconds(destroyTime);
    Destroy(gameObject);
}

我想破坏一个对象,然后再次创建另一个对象 你可以给我一个统一的代码吗我只有 销毁命令

此代码在我想要的特定时间后销毁对象 再次在该位置重新生成我的特定对象

1 个答案:

答案 0 :(得分:1)

如果你想在之后重新使用你的对象,那么最好的办法就是不要破坏它,但要禁用它。

var destroyTime : int; // This is the time in seconds
function Start(){
    yield WaitForSeconds(destroyTime);
    gameObject.SetActive(false);
}

然后,您可以再次重新启用它。但是,当你这样做并希望保持“在产生它后xx秒后销毁”时,你必须将你的函数更改为“OnEnable”事件,因为该对象已经启动。就像这样:

var destroyTime : int; // This is the time in seconds
function OnEnable(){
    yield WaitForSeconds(destroyTime);
    gameObject.SetActive(false);
}

PS:我很确定你已经有一个脚本附加到某个游戏对象上,它产生了游戏对象,因为它需要做spwawn / destroy逻辑。我希望有所帮助。

相关问题