I have a prefab of a grid which contains 3 different tilemaps:

Main, Foreground, & Background

I want to reference the tilemaps inside a script of the prefab but can't figure out how to do that... I just need to edit the tiles in the tilemaps located inside the prefab.


I figured it out! Rather than pre-creating the tilemaps inside of the prefab, You just have to add the tilemaps after the prefab was created (in the game).

Reference: https://stackoverflow.com/questions/54044398/how-to-programmatically-add-tilemaps-to-a-grid

You can add the 3 tilemaps to the grid in the awake function and use their variables:

    GameObject go = new GameObject("Main");
    mainTileMap = go.AddComponent<Tilemap>();
    TilemapRenderer tileRenderer = go.AddComponent<TilemapRenderer>();

    mainTileMap.tileAnchor = new Vector3(0, 0, 0);
    tileRenderer.sortingLayerName = "Default";

and do this for every tilemap. The 'mainTileMap' GameObject will store the reference to the tilemap.

  • \$\begingroup\$ You do not have to create the tilemaps at runtime for this to work. \$\endgroup\$ – DMGregory Jul 2 '20 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.