1

ive got this class it has one 2dArray and when im trying to fill it im getting the error NullReferenceException: Object reference not set to an instance of an object AdminGrid.FullNot(Int32 Row, Int32 Column, Int32 Full, System.String PieceName)

public class AdminGrid : MonoBehaviour {

    public int numRows;
    public int numColumns;
    private int[,] ArrayGrid;
    // Use this for initialization
    void Init() {
                numColumns = 6;
                numRows = 6;
        ArrayGrid = new int[numRows,numColumns];

        for(int y = 0;y < numRows;y++)
        {
            for(int x = 0;x < numColumns;x++)
            {
                ArrayGrid[y,x] = 0; 
            }
        }
    }

    public void FullNot(int Row,int Column,int Full,string PieceName)
    {   
        ArrayGrid[Row,Column] = 1;//Error is here
    }

    public int WhatsonGrid(int Row,int Col)
    {
        return ArrayGrid[Row,Col];
    }
}

Any idea why this is happening? as you can see my array is filled with 0s i thought that would fix this but it seems like not,also i made sure the values are inside the array meaning the max for columns and rows are 5,5

im using the engine Unity so Init //actually called start

it gets called when the game starts as a rule thats why im pretty sure it is called

4

2 に答える 2

1

ArrayGridは参照型フィールドであるため、デフォルトでは null になるため、簡単に結論づけます。あなたは を呼び出していませんInit。だから:電話してInitください。

于 2012-05-19T18:50:59.800 に答える
1

Init の名前を AdminGrid に変更すると、すべて問題ありません)


public AdminGrid()
{
...
}
于 2012-05-19T19:07:44.940 に答える