2016-09-18 131 views
0

Rundown 好吧,家伙和gals需要一点帮助。基本上我是从一个开放的位置预制一个健康包。经过一段时间后,我试图摧毁HealthPack,如果它没有拿起,则将bool healthPackExist设置为false,并在免费位置实例化另一个HealthPack。销毁由脚本Unity3d C#中的脚本创建的对象#

问题: 当我试图访问游戏对象被实例化我最终要么摧毁整个父层次结构或只是擦脚本出来。

解决方案 我试图破坏根对象,搜索创建的对象的名称,添加标签的对象(健康套餐),并寻找它总是得到错误。

代码如下:

public GameObject healthPackPrefab; 
public GameObject health; 
private float healthTimer; 
private bool healthExist; 

// Use this for initialization 
void Start() 
{ 
    //set healthExist to false to indicate no health packs exist on game start 
    healthExist = false; 
} 

// Update is called once per frame 
void Update() 
{ 
    //first check to see if a health pack exist, if not call method to spawn a health pack 
    //otherwise check to see if one exist and if it does is it's timer created with it 
    //has been passed by Time.time (returns game clock time), if yes destroy the created 
    //health pack. 
    if (healthExist == false) 
    { 
     spawnUntilFull(); 
    } 
    else if (healthExist == true && Time.time > healthTimer) 
    { 
     //Error occuring here when trying to destroy 
     //Destroy(transform.root.gameObject) //destroys script, object scripts on, and prefab 
     Destroy(this.gameObject); //does same as Destroy(transform.root.gameObject 
     healthExist = false; 
    } 
} 



Transform NextFreePosition() 
{ 
    //free space 
    foreach (Transform childPositionGameObject in transform) 
    { 
     //if no health packs located return location of child object to spawn new health pack 
     if (childPositionGameObject.childCount == 0) 
     { 
      return childPositionGameObject; 
     } 
    } 

    return null; 
} 

void spawnUntilFull() 
{ 
    //returns next free position in space 
    Transform freePosition = NextFreePosition(); 

    //if free slot is available 
    if (freePosition && healthExist == false) 
    { 
     //instantiates health object and places it in scene at coordinates received from 
     //freePosition 
     health = Instantiate (healthPackPrefab, freePosition.position, Quaternion.identity) as GameObject; 

     //spawns enemy onto a position under the Parent (Enemy Formation) 
     health.transform.parent = freePosition; 

     //set bool to true to stop spawning 
     healthExist = true; 

     //seat HealthTimer to 5 seconds after creation for use in Update method 
     healthTimer = Time.time + 5.0f; 
    } 
} 
+0

你只是试图创建一个对象?你的代码'打开位置'和'spawnUntilFull'使得它听起来像你想生成多个,但你的代码似乎只允许创建一个,因为你将healthExist更改为true。 –

+0

我现在将它打开,根据时间和敌人的情况随机创建它们。这就是为什么当它被玩家拾起(不同的脚本)时,它会被销毁并更改为healthExist = false,然后在上面的脚本中,当定时器耗尽时,它应该被销毁以允许稍后再创建。 – Phillipv20

回答

2

,当你调用销毁您实际上做什么()被破坏脚本。为了实现你想要的(破坏健康包),你只需要它来打电话销毁,而不是:

Destroy(health); 

在一个侧面说明,以避免乱码使用Time.time,destroy()方法有一个使用两个参数的重载:

Destroy(GameObject object, float delay); 

这应该有助于简化代码并使其更具可读性。

+0

我想过,问题是我实际上没有附加到预制的脚本。预制只从上面的脚本实例化。感谢关于销毁的提示,我知道它在那里,我只是没有开始优化。 – Phillipv20

+0

有没有可能是一个游戏对象上的脚本能够销毁()它。 –

+0

无视以上我看到了你的意思并修正了代码,现在它正在工作,只是在玩家抓住它时遇到一个小问题,它在后来发生破坏并发送另一个问题,但未注册健康提升,直到与其他事件发生碰撞。 – Phillipv20

0

您可以使用您的自定义DestroyObject方法在预制件上添加单独的脚本。

只要对象被创建,就在该脚本上启动一个定时器。

现在,如果它没有在一定的时间内收集,你可以很容易地摧毁对象。