C# 从脚本Unity3d C中销毁脚本创建的对象#

C# 从脚本Unity3d C中销毁脚本创建的对象#,c#,unity3d,unity5,C#,Unity3d,Unity5,详细说明 好了,伙计们需要一些帮助。基本上,我是从一个处于打开位置的预置中实例化一个healthPack。经过一段时间后,如果没有拾取HealthPack,我将尝试销毁它,将bool healthpackkexist设置为false,并在空闲位置实例化另一个HealthPack 问题: 当我试图访问正在实例化的游戏对象时,我要么破坏整个父层次结构,要么只是删除脚本 解决方案 我尝试过销毁根对象,搜索已创建对象的名称,向对象(Health Pack)添加标记,搜索它时总是出错 代码如下: publ

详细说明 好了,伙计们需要一些帮助。基本上,我是从一个处于打开位置的预置中实例化一个healthPack。经过一段时间后,如果没有拾取HealthPack,我将尝试销毁它,将bool healthpackkexist设置为false,并在空闲位置实例化另一个HealthPack

问题: 当我试图访问正在实例化的游戏对象时,我要么破坏整个父层次结构,要么只是删除脚本

解决方案 我尝试过销毁根对象,搜索已创建对象的名称,向对象(Health Pack)添加标记,搜索它时总是出错

代码如下:

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;
    }
}

当您调用Destroy()时,实际上正在销毁脚本。要实现您想要的(销毁健康包),您只需调用销毁:

Destroy(health);
另一方面,为了避免使用
Time.Time
,Destroy()有一个重载,它使用两个参数:

Destroy(GameObject object, float delay);

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

调用Destroy()时有效地执行的操作是销毁脚本。要实现您想要的(销毁健康包),您只需调用销毁:

Destroy(health);
另一方面,为了避免使用
Time.Time
,Destroy()有一个重载,它使用两个参数:

Destroy(GameObject object, float delay);

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

您可以在预置上添加一个单独的脚本,其中包含自定义DestroyObject方法

创建对象后,立即在该脚本上启动计时器


现在,如果在一定时间内没有收集到,您可以轻松销毁该对象。

您可以在预置上添加一个单独的脚本,其中包含自定义的DestroyObject方法

创建对象后,立即在该脚本上启动计时器


现在,如果在一定时间内没有收集到,您可以很容易地销毁该对象。

您是否仅尝试创建一个对象?你的代码“open position”和“spawnUntilFull”听起来像是你想要生成多个,但你的代码似乎只允许创建一个,因为你将healthExist更改为true。我现在将其打开,以便根据时间随机创建它们,并摧毁敌人。这就是为什么当玩家(不同的脚本)拾取它时,它会被销毁并更改为healthExist=false,然后在上面的脚本中,当计时器用完时,它会被销毁以允许稍后进行另一次创建。您是否只尝试创建一个对象?你的代码“open position”和“spawnUntilFull”听起来像是你想要生成多个,但你的代码似乎只允许创建一个,因为你将healthExist更改为true。我现在将其打开,以便根据时间随机创建它们,并摧毁敌人。这就是为什么当它被玩家(不同的脚本)拾取时,它被销毁并更改为healthExist=false,然后在上面的脚本中,当计时器用完时,它应该被销毁,以允许稍后进行另一个创建。我想了想,问题是我实际上没有一个脚本附加到预置。预设仅从上述脚本实例化。感谢关于销毁的提示,我知道它就在那里,我只是还没有开始优化。游戏对象上不必有脚本就可以销毁()它。不管上面说什么,我明白了你的意思,并修复了代码,它现在正在工作,只是在玩家捕获它时出现了一个小问题,它会在稍后销毁并发送另一个,但在与其他东西发生碰撞之前不会注册生命值提升。为此,请发布一个单独的问题。我想了想,问题是我实际上没有将脚本附加到预置。预设仅从上述脚本实例化。感谢关于销毁的提示,我知道它就在那里,我只是还没有开始优化。游戏对象上不必有脚本就可以销毁()它。不管上面说什么,我明白了你的意思,并修复了代码,它现在正在工作,只是在玩家捕获它时出现了一个小问题,它会在稍后销毁并发送另一个,但在与其他东西发生碰撞之前,它不会记录生命值提升。为此,请发布一个单独的问题。