IEnumerator timedelay()
{
yield return new WaitForSeconds(3f);
Debug.Log("sdfaDF");
br = Instantiate(a ,new Vector3 (-4,-6,0), Quaternion.identity ) as GameObject;
br.tag = "a";
makebox();
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "a")
{
Destroy(other.gameObject);
gm.mylife -= 1;
}
}
如果原始预制件遇到OnTriggerEnter2D,它就会销毁......然后克隆预制件停止创建。我需要制作克隆的预制件,即使原来的预制件被销毁也不会被销毁。
答案 0 :(得分:0)
使用您提供的代码进行调试非常困难,但我可以想到1个场景。确保你不在其中。
您可能已经销毁了原始预制件,而不是克隆件,我的意思是编辑器中项目标签中的实际预制件。
[SerializeField] GameObject originalPrefab; // You dragged & dropped your prefab here
var clone1 = Instantiate(originalPrefab);
var clone2 = Instantiate(originalPrefab);
var clone3 = Instantiate(originalPrefab);
var clone4 = Instantiate(originalPrefab);
Destroy(originalPrefab); // If you do this you destroy everything.
相反,试试这个:
[SerializeField] GameObject originalPrefab;
var baseClone = Instantiate(originalPrefab);
var clone1 = Instantiate(baseClone);
var clone2 = Instantiate(baseClone);
var clone3 = Instantiate(baseClone);
Destroy(baseClone); // Only destroy baseClone
如果你想销毁所有克隆,你可以将它们保存在一个数组中,然后循环遍历每个上调用Destroy()
的数组。