0

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.

buckybuck
  • 25
  • 3
  • 2
    Either `tileDataMap[x, y].temperatureType` is null, or you are checking if this is null and you then access in the else block another variable: `tileDataMap[x, y].humidityType` which is null. – Oguz Ozgul Mar 21 '20 at 13:40
  • 2
    Shouldn't you change the if statement to `if (tileDataMap[x, y].humidityType == null)` ? Because color being null will not throw a null pointer exception. – Oguz Ozgul Mar 21 '20 at 13:42
  • Just debug and put a breakpoint at the start of the offending block and see who is null :) – Oguz Ozgul Mar 21 '20 at 13:43
  • Yeah a small error from me so I change the check for ```if (tileDataMap[x, y].humidityType.color == null)``` but that doesn't change anything... – buckybuck Mar 21 '20 at 13:44
  • Oh sorry it does change something. Now I get the same error but for that check : ```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:65)``` which is ```if (tileDataMap[x, y].humidityType.color == null)```... How can I debug that with a breakpoint ? Not sure what's that mean – buckybuck Mar 21 '20 at 13:47
  • 1
    But as I said, please change it to `if (tileDataMap[x, y].humidityType == null)` because what is null is not the color but the containing instance, humidityType. – Oguz Ozgul Mar 21 '20 at 13:49
  • Oh yes thanks I didn't read your second comment well and when I check for humidityType it does appears that some value are null : I'd forget to define a small range elsewhere... So it's fixed. – buckybuck Mar 21 '20 at 13:51
  • May be you also should check the statement where you assign the humidityType. You only assign humidityType if humidityHeight is lower than the element's, then, there is probably no humidity type matching the height of your instance hence it is not assigned and is left null. – Oguz Ozgul Mar 21 '20 at 13:51

0 Answers0