Unity List 添加的每个对象在它们应该不同时似乎是相同的项目......?

Unity List every object added appears to be the same item when they should be different...?

目前我正在尝试为我正在创建的游戏制作一个动态列表,基本上创建了一个菱形图案,如下所示。

并且我希望能够将这些对象的位置保存在 array/list 中,但是正如您所看到的每列的长度大小不同,所以我认为列表比数组更好(但如果你有动态数组提示请帮忙)。但是,当我通过 for 循环将对象添加到我的列表时,我最终会在列表中一遍又一遍地复制完全相同的对象,即使我在将它添加到列表后更改了它。

void Start()
    {
        for(int i = 0; i < colNum; i++){ //Cycle through columns of numbers 2,3,4,5,6,7,8,9,10,11,12
            climbColumn.Add(new List<GameObject>()); //Create a 2D array list
            if( i < colNum/2){ //Check that you are in the first half of the columns
                for(int c = 0; c < ((i * 2) + gameLength) ; c++){  //cycle through rows (2 has 3 rows in a regular game, 3 has 5 rows in a regular game... etc)
                   SpawnClimbSpots(i,c);
                }
            }else{
                for(int c = 0; c < (-2 * i + 20 + gameLength) ; c++){ //cycle through rows (12 has 3 rows in a regular game, 11 has 5 rows in a regular game... etc)
                   SpawnClimbSpots(i,c);
                }
            }
            
        }
       
        dice = new Die[] //Create 4 dice
        {
            new Die(tempObj),  new Die(tempObj),  new Die(tempObj),  new Die(tempObj)
        };
         for(int i = 0; i < dice.Length; i++){ //assign the 4 die with the proper gameobjects.
            if(dice[i].dieGameObject.tag == "Untagged"){
                dice[i].dieGameObject = GameObject.FindGameObjectWithTag("Die" + (i + 1));
            }
         }
    }
    void SpawnClimbSpots(int i, int c){
        GameObject temp = Instantiate(climbSpot); //create new temp object for climbspot
         if( i < colNum/2){ //Check that you are in the first half of the columns
                    
                    climbColumn[i].Add(temp); //add to list
                    climbColumn[i][c].name = (i.ToString()+c.ToString());
                    climbColumn[i][c].transform.Find("Number").GetComponent<SpriteRenderer>().sprite = numSprites[i];
                   //temp.GetComponentInChildren<SpriteRenderer>().sprite = numSprites[i];
                    climbColumn[i][c].transform.position = new Vector2 (gameObject.transform.position.x - 10 + (i * 2), gameObject.transform.position.y + 1.5f - (c*1 - i) * 1.5f); //set position of newly created object
                    Instantiate(climbColumn[i][c]);//create object
                  
         }else{
                    climbColumn[i].Add(temp);//add to list
                    climbColumn[i][c].name = (i.ToString()+c.ToString());
                    climbColumn[i][c].transform.Find("Number").GetComponent<SpriteRenderer>().sprite = numSprites[i];
                    climbColumn[i][c].transform.position = new Vector2 (gameObject.transform.position.x - 10 + (i * 2), gameObject.transform.position.y + 1.5f  - (c*1 + i - 10)* 1.5f );//set position of newly created object
                    Instantiate(climbColumn[i][c]); //create object
         }
    }

到目前为止,我已经尝试在它自己的函数中创建 'temp' 对象,这样每次 forloop 运行时都会创建一个新对象,但这没有帮助。我 运行 没有办法解决它。

感谢您提供的任何帮助! :D

您正在复制 引用GameObject 是一个 class 所以行

GameObject temp = climpSpot;

不是你想的那样create new temp object for climpSpot

现在 tempclimpSpot 指的是同一个 GameObject


我认为您宁愿创建新对象,然后将其添加到相应的集合

GameObject temp = Instantiate(climpSpot);

并删除其他 Instantiate 个调用。


另请注意,您的 if -else 块非常相似,您应该将它们合并在一起,并且只检查它们之间实际不同的一行:

climpColumn[i][c].transform.position = new Vector2 (transform.position.x - 10 * (i + 2), transform.position.y + 1.5f - (i < colNum/2 ? c - i : c + i - 10) * 1.5f;