I'm working on a map generator (on Unity) which was so far drawing a height map. I tried to add two others height maps for the temperature and the humidity : both works the exact same way but when I try to draw the humidity map I get thrown that NullReferenceException.
How my code works :
I have a HeightMap class in which I store vars to generate a NoiseMap from which I build three others classes (TerrainMap, TemperatureMap and HumidityMap) with specifics variables but the two last one are alike except they a var (TemperatureType and HumidityType) inhereted from the same HeightType Class. TerrainType is also the same. Not sure I need to post that here.
Then I have a TileData class with the three heights and three types. After I generate my three heights map with the same function but their own vars I build a tileDataMap with those :
tileDataMap[x, y] = new TileData(x, y, terrainMap.heightMap[x, y], temperatureMap.heightMap[x, y], humidityMap.heightMap[x, y]);
Then depending on the heights I give the tile its types :
// setting humidity by type
for (int i = 0; i < humidityMap.humidityTypes.Length; i++)
{
if (tileDataMap[x, y].humidityHeight <= humidityMap.humidityTypes[i].height)
{
tileDataMap[x, y].humidityType = humidityMap.humidityTypes[i];
break;
}
}
// setting temperature by type
for (int i = 0; i < temperatureMap.temperatureTypes.Length; i++)
{
if (tileDataMap[x, y].temperatureHeight <= temperatureMap.temperatureTypes[i].height)
{
tileDataMap[x, y].temperatureType = temperatureMap.temperatureTypes[i];
break;
}
}
I then send my tileDataMap to my DrawingTexture fonction
case DrawMap.Humidity:
if (tileDataMap[x, y].temperatureType.color == null)
{
pixels[x + y * width] = Color.magenta;
}
else
{
pixels[x + y * width] = tileDataMap[x, y].humidityType.color;
}
break;
case DrawMap.Temperature:
pixels[x + y * width] = tileDataMap[x, y].temperatureType.color;
break;
The second one works normally (same thing for terrain) but not the first. You see here I try to handle a null exception (not sure it's supposed to be done that way) but that doesn't change anything. And before drawing my map I tried to check the values that I want to draw for both maps and in both case I get a color value...
Debug.Log(tileDataMap[0, 0].humidityType.color); // Print : RGBA(0.333, 1.000, 1.000, 0.000)
Debug.Log(tileDataMap[0, 0].temperatureType.color); // Print : RGBA(1.000, 0.047, 0.000, 0.000)
Debug.Log(tileDataMap[130, 130].humidityType.color); // etc.
Debug.Log(tileDataMap[130, 130].temperatureType.color);
Debug.Log(tileDataMap[width - 1, height - 1].humidityType.color);
Debug.Log(tileDataMap[width - 1, height - 1].temperatureType.color);
Edit : I forgot to say that the error point to this line :
pixels[x + y * width] = tileDataMap[x, y].humidityType.color;
NullReferenceException: Object reference not set to an instance of an object TextureGenerator.GetTexture (TileData[,] tileDataMap, DrawMap drawMap, System.Boolean drawUnderwaterSteps) (at Assets/Scripts/World Generator v1/TextureGenerator.cs:71)
So yeah it doesn't making any sense to me. I don't understand from where the problem could come from so I don't even know what part of my code should I share to help. I mean I build the two maps the same way and at the same time, it would be better if both worked or not, not only one of them... Maybe it's some kind of bug ? I don't understand. I'll appreciate any help.