【问题标题】:I wrote a code with instantiate and it copies prefab to the scene. I wanna delete it in a scene, not a prefab. How do I?我用实例化编写了一个代码,它将预制件复制到场景中。我想在场景中删除它,而不是预制件。我如何能?
【发布时间】:2022-11-10 22:01:51
【问题描述】:

游戏对象 b 是一颗不会杀死的流星,但会显示真正的流星(a)会在哪里产卵。继承人的代码:

 IEnumerator hard()
    {   
        
        while(true)
        {   
            x1 = Random.Range(-10, 10);
            y1 = Random.Range(6, -6);
            x2 = Random.Range(-10, 10);
            y2 = Random.Range(6, -6);
            x3 = Random.Range(-10, 10);
            y3 = Random.Range(6, -6);
            Instantiate(b, new Vector3(x1, y1, 0), Quaternion.identity);
            Instantiate(b, new Vector3(x2, y2, 0), Quaternion.identity);
            Instantiate(b, new Vector3(x3, y3, 0), Quaternion.identity);
            yield return new WaitForSeconds(2);
            Instantiate(a, new Vector3(x1, y1, 0), Quaternion.identity); 
            Instantiate(a, new Vector3(x2, y2, 0), Quaternion.identity);
            Instantiate(a, new Vector3(x3, y3, 0), Quaternion.identity);   
        }
    }


    IEnumerator clean()
    {
        Destroy(b, 1);
        yield return new WaitForSeconds(5);
    }

错误信息是:

不允许破坏资产以避免数据丢失。 如果您真的想删除资产,请使用 DestroyImmediate (theObject, true);

我不需要销毁资产,只销毁场景中的游戏对象,如何?

即使我不需要它,我也尝试使用 DestroyImmediate(),但它仍然说没有权限。

【问题讨论】:

  • 不确定,但可能创建一个实例化对象的游戏对象变量,然后销毁游戏对象变量应该可以工作。试试这个:Gameobject gameObjectB = Instantiate(b, new Vector3(x1, y1, 0), Quaternion.identity); 然后Destroy(gameObjectB, 1)

标签: c# unity3d


【解决方案1】:

Instantiate 返回对生成的游戏对象的引用。您需要将其保存在临时变量中并在适当的时间销毁该对象。

 IEnumerator hard()
{   
    while(true)
    {   
        x1 = Random.Range(-10, 10);
        y1 = Random.Range(6, -6);
        x2 = Random.Range(-10, 10);
        y2 = Random.Range(6, -6);
        x3 = Random.Range(-10, 10);
        y3 = Random.Range(6, -6);
        GameObject temp1 = Instantiate(b, new Vector3(x1, y1, 0), Quaternion.identity);
        GameObject temp2 = Instantiate(b, new Vector3(x2, y2, 0), Quaternion.identity);
        GameObject temp3 = Instantiate(b, new Vector3(x3, y3, 0), Quaternion.identity);
        yield return new WaitForSeconds(2);
        Destroy(temp1);
        Destroy(temp2);
        Destroy(temp3);
        Instantiate(a, new Vector3(x1, y1, 0), Quaternion.identity); 
        Instantiate(a, new Vector3(x2, y2, 0), Quaternion.identity);
        Instantiate(a, new Vector3(x3, y3, 0), Quaternion.identity);   
    }
}

或者,由于您提到协同程序可以在清理之前停止,您可以给预制标签并使用GameObject.FindGameObjectsWithTag 函数在您进行清理时在场景中找到它们。

void Clean(){
  GameObject[] trash = GameObject.FindGameObjectsWithTag("Trash");
  foreach(GameObject item in trash){
    Destroy(item);
  }
}

在此示例中,当您调用 Clean 函数时,场景中带有“垃圾”标签的所有对象都将被删除。

【讨论】:

  • 你可能没有得到它的权利。当我将游戏模式从 hard() 更改为 normal() 或 easy() 时,我使用函数 StopAllCoroutines() 以便一次不超过 3 个实例,所以这个 Destroy(temp) 不起作用。无论如何,感谢您的帮助,我尝试自己解决这个问题<3
  • @GoTouchGrass 我已经更新了我的答案,它不依赖于协程的完成。
猜你喜欢
  • 1970-01-01
  • 2021-12-09
  • 1970-01-01
  • 1970-01-01
  • 2014-07-24
  • 2018-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多