【问题标题】:Unity List every object added appears to be the same item when they should be different...?Unity List 添加的每个对象在它们应该不同时似乎是同一个项目......?
【发布时间】:2021-07-01 16:18:12
【问题描述】:

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

并且我希望能够将这些对象的位置保存在数组/列表中,但是当您看到每列的长度大小不同时,我认为列表会比数组更好(但如果您有动态数组提示请帮忙)。但是,当我通过 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

【问题讨论】:

    标签: c# list unity3d multidimensional-array dynamic


    【解决方案1】:

    您正在复制参考GameObject 是一个,所以与行

    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;
    

    【讨论】:

    • 太棒了,我更改了那行代码,它运行良好!非常感谢您的帮助!
    • @Tyler 您可以通过单击答案左上角的绿色复选标记来感谢某人;这样做会将您的问题标记为已回答。在标题中添加“(已解决)”并不会将问题标记为已回答。
    猜你喜欢
    • 1970-01-01
    • 2016-01-29
    • 1970-01-01
    • 1970-01-01
    • 2020-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-15
    相关资源
    最近更新 更多