Hello Guest

Author Topic: NullReferenceException in tk2dTileMapBuilderUtil  (Read 4384 times)

r0l3k

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 7
    • View Profile
NullReferenceException in tk2dTileMapBuilderUtil
« on: February 05, 2015, 04:08:34 pm »
Hi,

Suddenly I started getting this error. (possibly after a collider update to one of the prefabs).

NullReferenceException
tk2dRuntime.TileMap.BuilderUtil.HideTileMapPrefabs (.tk2dTileMap tileMap) (at Assets/TK2DROOT/tk2dTileMap/Code/tk2dTileMapBuilderUtil.cs:280)
tk2dTileMap.ClearSpawnedInstances () (at Assets/TK2DROOT/tk2dTileMap/Code/tk2dTileMap.cs:225)
tk2dTileMap.Build (BuildFlags buildFlags) (at Assets/TK2DROOT/tk2dTileMap/Code/tk2dTileMap.cs:304)
tk2dTileMap.EndEditMode () (at Assets/TK2DROOT/tk2dTileMap/Code/tk2dTileMap.cs:526)
tk2dTileMap.Awake () (at Assets/TK2DROOT/tk2dTileMap/Code/tk2dTileMap.cs:117)
UnityEditor.DockArea:OnGUI()


After debugging the code

for (int i = 0; i < instListCount; ++i) {
            int x, y, layerIdx;
            GameObject instance;
            tileMap.GetTilePrefabsListItem(i, out x, out y, out layerIdx, out instance);
            
            // Is it already IN the list for some reason?
            if (!instExists) {
               int tileId = (x >= 0 && x < tileMap.width && y >= 0 && y < tileMap.height) ? tileMap.GetTile(x, y, layerIdx) : -1;
               if (tileId >= 0 && tileId < prefabs.Length && prefabs[tileId] != null) {
                  instExists = true;
               }
            }
            
            if (instExists) {
               tileX.Add(x);
               tileY.Add(y);
               tileLayer.Add(layerIdx);
               tileInst.Add(instance);

               tk2dUtil.SetTransformParent(instance.transform, tileMap.PrefabsRoot.transform);
            }
         }

in this line: tk2dUtil.SetTransformParent(instance.transform, tileMap.PrefabsRoot.transform);
instance is null.

How to find a broken tile ?
« Last Edit: February 05, 2015, 04:15:47 pm by r0l3k »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: NullReferenceException in tk2dTileMapBuilderUtil
« Reply #1 on: February 05, 2015, 09:21:57 pm »
I think add if (instExists && instance != null), that should get you past the error and let you fix it in the next commit?
This could happen if the object was deleted in the cached instance list while in edit mode...

r0l3k

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: NullReferenceException in tk2dTileMapBuilderUtil
« Reply #2 on: February 05, 2015, 09:50:37 pm »
Thanks.

biggo

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: NullReferenceException in tk2dTileMapBuilderUtil
« Reply #3 on: May 04, 2015, 09:03:50 pm »
I have a similar problem where TilemapPrefabInstances sometimes are null:

I use a scene for each level. There's one TileMap per scene. All TileMaps are using the same SpriteCollection, tileMapData and tileMapEditorData, and they are all drawn the same way, with the same 4 prefabs placed onto the map.

From code I get to the prefab instance like so:             
var tilemapPrefabInstance = CurrentTileMap.TilePrefabsList.Single(i => i.instance.tag == "TagOfTheGameObject");

But in some scenes the TilemapPrefabInstanc.instance suddently is null. I have no idea when and why this happens.
I've tried editing and committing, unlinking the TileMap Render Data, and the advice mentioned here, but nothing helps. It's like, when the error appear, there's no way of getting rid of it again.

Any advice, what I could try next? ???

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: NullReferenceException in tk2dTileMapBuilderUtil
« Reply #4 on: May 06, 2015, 10:28:46 pm »
Can you get me a repro when its broken?