英文:
assigning a GameObject to a 2D array of GameObjects return null
问题
我有一个GameObject的二维数组,我想要从两个循环中分配一个GameObject,但是分配返回null。
void PlaceBricks(){
placedBricksPositions = new GameObject[columnNumber * 2, rowNumber * 2]; // 倍增大小以确保适应
FixBorders();
List<GameObject> bricksWithRates = GetBrickWithRatesList();
int xCounter = 0;
for (float x = topRight.x; x >= bottomLeft.x; x -= (topRight.x - bottomLeft.x) / columnNumber){
int yCounter = 0; Debug.Log("hola");
for (float y = topRight.y; y >= bottomLeft.y; y -= (topRight.y - bottomLeft.y) / rowNumber){
GameObject brickToSpawn = GetBrickToSpawn(bricksWithRates);
placedBricksPositions[xCounter, yCounter++] = Instantiate(brickToSpawn, new Vector3(x, y, 0), Quaternion.identity);
Debug.Log(placedBricksPositions[xCounter, yCounter]);
}
xCounter++;
}
}
游戏对象正常实例化,将其分配给普通GameObject也正常工作,但将该GameObject分配给数组时也返回null。
英文:
I have a 2D array of GameObjects, and I want to assign a GameObject from 2 for loops, but the assignment returns null.
void PlaceBricks(){
placedBricksPositions=new GameObject[columnNumber*2,rowNumber*2]; //doubled the size just to make sure it fits
FixBorders();
List<GameObject> bricksWithRates=GetBrickWithRatesList();
int xCounter=0;
for(float x=topRight.x;x>=bottomLeft.x;x-=(topRight.x-bottomLeft.x)/columnNumber){
int yCounter=0;Debug.Log("hola");
for(float y=topRight.y;y>=bottomLeft.y;y-=(topRight.y-bottomLeft.y)/rowNumber){
placedBricksPositions[xCounter,yCounter++]=Instantiate(
GetBrickToSpawn(bricksWithRates),new Vector3(x,y,0),Quaternion.identity);
Debug.Log(placedBricksPositions[xCounter,yCounter]);
}
xCounter++;
}
}
the gameobjects are being instantiated normally, and assigning it to a normal gameobject works, but when assigning that gameobject to the array it also returns null.
答案1
得分: 0
我增加了yCounter然后使用它进行调试,但它还没有被分配,所以这是一个调试问题。
英文:
i incremented yCounter then debugged using it, while it wasn't assigned yet, so it was a debug problem
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论