简体   繁体   English

将对象添加到Array Unity3d C#的奇怪行为#

[英]Strange behavior adding objects to Array Unity3d C#

I am seeing some odd behavior, when adding objects to an array of GameObjects. 在将对象添加到GameObjects数组时,我看到了一些奇怪的行为。 Here is my sudo code: 这是我的sudo代码:

public class GameController : MonoBehaviour {
    public GameObject[] MyArray;

   void Awake() {
       Application.runInBackground = true;
       MyArray= new GameObject[144];
   }
    //some code that calls myFunction
   public  IEnumerator myFunction(float time)   
    {
       int counter = 0;
       GameObject card= AnotherArrayOfGameObjects[0];
       for (int j = 0; j < 144; j++)
        {
             card.name = "BLANK_" + (j+1);           
            Instantiate(card, cardVector, Quaternion.Euler(0, rotateAdujust, 180));
            MyArray[counter] = card;
            yield return new WaitForSeconds(time);
            counter++;
        }
    }

} 

This function kind of works ok. 这个功能很好用。 It creates my object and instantiates it. 它创建我的对象并实例化它。 But, when it adds the object "card" to the array MyArray, it does not add it correctly. 但是,当它将对象“card”添加到数组MyArray时,它不会正确添加它。 Ie, as this is in a loop, 0-143, I would expect the array to look like this: 即,因为这是一个循环,0-143,我希望数组看起来像这样:

MyArray[0] = "BLANK_1" 
MyArray[1] = "BLANK_2" 
....
MyArray[143] = "BLANK_144" 

Instead, it looks like this: 相反,它看起来像这样:

MyArray[0] = "BLANK_144" 
MyArray[1] = "BLANK_144" 
....
MyArray[143] = "BLANK_144" 

I can see all this, by the way, because MyArray is a public variable in my code, so I can see it in Unity's Inspector. 顺便说一句,我可以看到这一切,因为我的代码中的MyArray是一个公共变量,所以我可以在Unity的Inspector中看到它。 If I debug, I can see each iteration overwrite the previous. 如果我调试,我可以看到每次迭代覆盖前一次。 So, in the first iteration of the loop, i get: 所以,在循环的第一次迭代中,我得到:

   MyArray[0] = "BLANK_1" 

Then in the second iteration of the loop, I get: 然后在循环的第二次迭代中,我得到:

   MyArray[0] = "BLANK_2" 
   MyArray[1] = "BLANK_2" 

In the Third: 在第三个:

   MyArray[0] = "BLANK_3" 
   MyArray[1] = "BLANK_3" 
   MyArray[2] = "BLANK_3" 

Until it completes the full loop and all items in the array say "BLANK_144" 直到它完成完整循环并且数组中的所有项目都说“BLANK_144”

Can anyone explain why this is happening? 任何人都可以解释为什么会这样吗?

You are adding same card object to array on each iteration. 您在每次迭代时将相同的card对象添加到数组。 And you are changing name of this object on each iteration. 并且您在每次迭代时更改此对象的名称。 Thus you end up with array, all items of which are pointing to same card instance. 因此,您最终会得到数组,其中所有项都指向同一个卡实例。 And that card instance whill have latest name which you have assigned (on last iteration): 并且该卡实例具有您已分配的最新名称(在最后一次迭代中):

MyArray[counter] = card;

Remember - Instantiate method returns cloned object, but it does not change the object which you are cloning. 记住 - Instantiate方法返回克隆对象,但它不会更改您正在克隆的对象。 So you instantiate new clone on each iteration, but you don't save it anywhere. 因此,您在每次迭代时实例化新克隆,但不要将其保存在任何位置。

You should add to array card clones which you are instantiating: 您应该添加到要实例化的阵列卡克隆:

GameObject card = AnotherArrayOfGameObjects[0];
for (int j = 0; j < 144; j++)
{
    var cardClone = Instantiate(card, cardVector, Quaternion.Euler(0, rotateAdujust, 180));
    cardClone.name = "BLANK_" + (j+1); 
    MyArray[j] = cardClone;
    yield return new WaitForSeconds(time);
}

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

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