I have a list of grid classes which contains a list of __l__and classes .I get a object reference is set to null reference exception error when I try to add something to the land class. I can’t seem to figure out what could be the problem*.* Here is the map creator script :
//creating the grid
grid myGrid = new grid();
//creating the city block/grid
blockNumber = Random.Range(0, denseCityBlocks.Length);
GameObject myBlock = Instantiate(denseCityBlocks[blockNumber], myBlocks[blockId].transform.position, transform.rotation);
//creating the elements in the city block
for (int i = 0; i < myBlock.GetComponent<cityBlock>().Lands.Length; i++)
{
land myLand = new land();
//saving the land to mygrid
myGrid.myLands.Add(myLand); //this is where I get error
// the file runs perfectly without this line
}
//saving the grid to game state manager
gameStateManager.GetComponent<gameState>().myGrids.Add(myGrid);
Here is the grid class:
[System.Serializable]
public class grid {
public int blockNumber;
public int blockRotation;
public List<land> myLands;
}
;
and the land class
[System.Serializable]
public class land {
public bool isEmpty;
public bool isOwned;
public GameObject building;
public bool hasBuildingOnIt;
public GameObject decoration;
public bool hasDecorationOnIt;
}
;
Can someone help me figuring out the problem? Thanks ![]()
PS: I edited out some lines of code to make it easier to look at from the mapcreator script. The code runs the same way without those lines. I could add them back if required.