【问题标题】:Problem in Unity when destroying an instantiated object- Destroys all clones of the original object销毁实例化对象时 Unity 中的问题 - 销毁原始对象的所有克隆
【发布时间】:2019-05-10 09:05:59
【问题描述】:

基本上,我正在尝试重新创建一种 DDR 类型的东西,我只是在尝试降低基本的游戏玩法。在游戏开发方面我仍然是新手,但我有一点经验。现在我正在实例化 4 个不同箭头方向的随机箭头,当我这样做时,我将它放在一个列表中。列表的第一个箭头始终是场景中的下一个箭头,我使用 RemoveAt() 来确保这一点。我遇到的问题是,当我在列表中的一行中获得两个相同的箭头方向(来自同一个原始预制件)时,如果它们是连续的,删除一个将删除所有它们。它只是这样做;例如,如果我的箭头顺序是左、左、下,它会删除两个左。如果我的箭头顺序是左、下、左,它只会删除第一个左边,我就是不知道为什么。我知道这不是名称,因为我每次都尝试更改它但没有任何效果,我尝试使用队列,我所做的一切我都无法让它工作,我需要帮助。

spawnedBox = Instantiate(spawningBox, new Vector3(spawnPos, -6, 0), Quaternion.identity) as GameObject;

arrows.Add(spawnedBox);

(不同的文件)

destroyArrow = FindObjectOfType<Blocks>().arrows[0];
FindObjectOfType<Blocks>().arrows.RemoveAt(0); Destroy(destroyArrow);

【问题讨论】:

  • 请编辑您的问题并添加您的代码的minimal reproducible example。没有它,我们无法为您提供帮助。
  • 添加删除游戏对象的代码
  • spawnedBox = Instantiate(spawningBox, new Vector3(spawnPos, -6, 0), Quaternion.identity) as GameObject; arrows.Add(spawnedBox); (不同的文件) destroyArrow = FindObjectOfType&lt;Blocks&gt;().arrows[0]; FindObjectOfType&lt;Blocks&gt;().arrows.RemoveAt(0); Destroy(destroyArrow); 抱歉这是我的第一篇文章我真的不知道如何格式化
  • 在您发布的代码中,您总是破坏数组的第一个元素(您传递的是索引 0)
  • 我想销毁索引 0,但它会删除所有相同类型的箭头,即使它们不在索引 0 中。

标签: c# unity3d


【解决方案1】:

实例化每个箭头并将其存储到一个数组中。然后当需要它时,如果从数组中销毁。你可以试试这样:

public GameObject[] ArrowsArray;

//Here you instantiate the arrow and you store it 
ArrowsArray[index] = Instantiate (originalPref, transform.position, Quaternion.identity) as GameObject;

//Here you destroy the gameObject and remove it from the array
Destroy(ArrowsArray[index]);
ArrowsArray.RemoveAt (index);

【讨论】:

  • List&lt;T&gt; 是一个东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多