将一个游戏对象分配给一个游戏对象的二维数组会返回 null。

huangapple go评论90阅读模式
英文:

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&lt;GameObject&gt; bricksWithRates=GetBrickWithRatesList();

    int xCounter=0;

    for(float x=topRight.x;x&gt;=bottomLeft.x;x-=(topRight.x-bottomLeft.x)/columnNumber){
        int yCounter=0;Debug.Log(&quot;hola&quot;);
        for(float y=topRight.y;y&gt;=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

huangapple
  • 本文由 发表于 2023年6月22日 06:33:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76527549.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定