简体   繁体   English

销毁实例化对象时Unity中的问题-销毁原始对象的所有克隆

[英]Problem in Unity when destroying an instantiated object- Destroys all clones of the original object

Basically, I'm trying to recreate a DDR kind of thing and I'm just trying to get the basic gameplay down. 基本上,我正在尝试重新创建DDR类的东西,而我只是想降低基本的游戏玩法。 I'm still a novice when it comes to game dev but I have a little bit of experience. 在游戏开发方面,我仍然是一个新手,但我有一点经验。 Right now I'm instantiating a random arrow of 4 different arrow directions, and when I do that I put it in a list. 现在,我实例化了4个不同箭头方向的随机箭头,然后将其放入列表中。 The first arrow of the list is always the next arrow in the scene, and I use RemoveAt() to ensure that. 列表中的第一个箭头始终是场景中的下一个箭头,我使用RemoveAt()来确保这一点。 The problem I'm getting is that when I get two of the same arrow directions (coming from the same original prefab) in a row in the list, deleting one will delete all of them if they are consecutive. 我得到的问题是,当我在列表中的一行中同时收到两个相同的箭头方向(来自相同的原始预制件)时,如果删除两个箭头,则它们将是连续的。 It only does it like this; 它只是这样做。 for example, if my arrow order is left, left, down, it deletes both lefts. 例如,如果我的箭头顺序是左,左,下,则删除两个左箭头。 If my arrow order is left, down, left, it only deletes the first left and I just can't figure out why. 如果我的箭头顺序是左,下,左,则只会删除第一个左箭头,而我只是不知道为什么。 I know it's not the names because I've tried changing that each time but that did nothing, I've tried using a queue, everything I do I just can't get it to work and I need help. 我知道这不是名字,因为我每次都尝试更改它,但是什么也没做,我尝试使用队列,我所做的一切都无法正常工作,我需要帮助。

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

arrows.Add(spawnedBox);

(different file) (不同的文件)

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

Instantiate each arrow and store it into an array. 实例化每个箭头并将其存储到数组中。 Then when required it, destroyed if from the array. 然后在需要时将其从阵列中销毁。 You can try something like this: 您可以尝试如下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM