【发布时间】: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