UnassignedReferenceException - but it IS assigned, and the code works!

Hi,

Having a strange issue here. When I run my code, it works - I’m manipulating a tilemap’s tiles. However, the console gives me the error in the title indicating that I’m missing a reference to the object. It’s the very object I’m manipulating (it works), so if the reference wasn’t there, the effect wouldn’t happen (which it does).

I’ve got 4 tilemaps on a grid. All of these tilemaps are referenced in the same place, on the same script, in the same manner:

    public Tilemap flatHexTilemap;  
    public Tilemap flatHexTilemapLevel2;
    public Tilemap flatHexTilemapLevel3;
    public Tilemap Fog;

Further down in my “UpdateLineOfSight()” method, I’m referencing 2 of those tilemaps:

if (flatHexTilemapLevel2.HasTile(localPlace))  //no error reference here
            {                
                Fog.SetTile(localPlace, null);   //error points to this line here

                print(flatHexTilemapLevel2.GetTile(localPlace).name + " ");   //again no error here
                print("X: " + localPlace.x.ToString());
                print("Y: " + localPlace.y.ToString());
            }

All 4 of those tilemaps are linked to the script (dragged in the inspector) from the same spot in the heirarchy to the same spot on the script, in the same manner. However, ONE of those tilemaps is generating an error, despite the code executing properly (it’s basically just poking holes in the fog of war map wherever there happens to be tiles on the level 2 map. Not ultimately what i need, but just for testing.)

The error says the Fog variable is missing the object reference. But… it really isn’t… lol.

Any ideas? This one is really weird.

Thanks

You may be setting it but you are probably accessing it before its set and thats why the exception is being thrown. Make sure to set it first.

Yeeaaaaaaa I’m a moron.

All this time, I had that same script accidentally attached to a SECOND object… in THAT object, the reference wasn’t dragged over. This also explains why the cellBounds.allPositionsWithin was returning twice as many tiles as it should’ve been and tanking performance.

Thanks for the help. I do appreciate it! :slight_smile: