I was hoping someone could tell me why I’m getting a NullReferenceException error with this code. I wrote something similar to this in JS and I’m trying to rework everything into C#. The error is coming from the line
mapTile.name = "Map Tile “+xLocation+”, "+zLocation; I have no clue why, I could do this in JS. If I comment it out, I get the same thing with the next line. How do I properly reference something I’ve just instantiated?
public void BuildMap () {
//BuildMap performs a loop which will create map tiles row by row, column by column, with dimensions xBoundary and yBoundary.
for(int i = 0; i < xBoundary*zBoundary; i++){
//determines the current tiles location in the grid.
xLocation = (int)i%xBoundary;
zLocation = (int)Mathf.Floor(i/xBoundary);
//Creates the map tile at coordinates xLocation, 1, zLocation, with no rotation. This object is now GameObject mapTile.
mapTile = Instantiate (mapTilePrefab, new Vector3(xLocation, 1, zLocation), Quaternion.identity) as Transform;
//Assigns a name to the map tile for the heirarchy.
mapTile.name = "Map Tile "+xLocation+", "+zLocation;
mapTileScript = mapTile.FindChild("Map Tile Collider").GetComponent(typeof(MapTileColliderBehaviour)) as MapTileColliderBehaviour;
//Assigns the tile a room value, and its x and z values.
mapTileScript.roomValue = 0;
mapTileScript.xGridValue = xLocation;
mapTileScript.zGridValue = zLocation;
}
}