(Solved) An arraylist error I couldn't* fix

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 :slight_smile:

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.

Hard to say without the exact error message and line number. Do you know which object it says has a null reference?
I’m guessing its this line here:

  for (int i = 0; i < myBlock.GetComponent<cityBlock>().Lands.Length; i++)

I can’t see the script for cityBlock, but I assume Lands is some List or array. When you instantiate a new myBlock is the script allocating space for the Lands with the new keyword?

Yes.

Lands is a array of gameobject in the city block prefab. It contains nothing else. Used to set position.

Figured out the problem, the nested myGrid.myLands had to be declared for each grid even after the class. I don’t think I am expressing myself correctly. But my fix was: 1. Declare a temporary new list. After creating all elements of the list pass it to myGrid.myLands using > myGrid.myLands = new List(templist);

My first time working with nested lists. Didn’t know nested lists work this way. [Added the fix incase someone runs into same problem(unlikely)]

In your constructor method for grid. Add myLands = new List<Land>();
Then you wont need to assign it in a different class.

1 Like

Ah, that’s what I needed to do. Thanks, I appreciate it. :slight_smile: