Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Instantiating/Loading tilemaps at runtime

Discussion in '2D Experimental Preview' started by Richardbmth, Nov 13, 2017.

  1. Richardbmth

    Richardbmth

    Joined:
    Mar 5, 2016
    Posts:
    30
    Hi,

    We are using the Tilemap functionality as grids for a puzzle game. We have over 200 levels and made each tilemap a prefab which we Instantiate into the scene using the following as a quick, easy way in a script Start()

    Code (CSharp):
    1. GameObject _prefab = Resources.Load <GameObject> ("TileBGPrefabs/" + levelNum);
    2. GameObject gridBgPrefab = (GameObject)Instantiate (_prefab, new Vector3(0f,0f,5f), Quaternion.identity);
    There’s no errors on the Editor but on Apple devices we get the following error (multiple times) , even though the tilemap shows fine:

    A script behaviour (probably UnityEngine.Tilemaps.Tile?) has a different serialization layout when loading. (Read 136 bytes but expected 160 bytes)

    Did you #ifdef UNITY_EDITOR a section of your serialized properties in any of your scripts?

    UnityEngine.Resources:Load(String)
    LevelScript:Start()



    I’m not sure if this is a Unity bug or if we are initialising the tilemaps in a manner not intended by the Unity developers. For our simple needs, storing each tilemap as a prefab and the using Resources.Load and Instantiating would have been fine though.

    So my question is, if we should do a bug report or if there’s a better method of loading a tilemap into a scene at runtime? Apologies if I've missed something obvious while researching this topic!

    Update: I tried the method of storing a Gameobject reference to the prefab and Instantiating that way but still have the same error.

    Thanks

    Rich
     
    Last edited: Nov 16, 2017
    rakkarage likes this.
  2. Richardbmth

    Richardbmth

    Joined:
    Mar 5, 2016
    Posts:
    30
    It occurred to me while walking away from the computer, that the UnityEngine may be complaining about the part of the tile map functionality which is accessed only by the editor i.e editing the tilemap, which of course is available when running from the editor but not when published to a device.
     
    Last edited: Nov 13, 2017
  3. Richardbmth

    Richardbmth

    Joined:
    Mar 5, 2016
    Posts:
    30
    I've submitted a bug report after producing a test file which replicates the problem. Hopefully it will be fixed or someone kindly shows me that you don't instantiate a tilemap/grid, you do it xyz way ;)
     
  4. moltow

    moltow

    Joined:
    Jun 24, 2017
    Posts:
    15
    FYI, I am getting the same errors on iOS and I am not instantiating the tile map via script.
     
  5. moltow

    moltow

    Joined:
    Jun 24, 2017
    Posts:
    15
    @Richardbmth, Did you receive any response from Unity regarding this bug? I'm wondering if we'll find it fixed in a patch release soon.

    Thanks.
     
  6. Richardbmth

    Richardbmth

    Joined:
    Mar 5, 2016
    Posts:
    30