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

Loading order of GameObjects in TileMap

Discussion in '2D Experimental Preview' started by 0710, Jul 19, 2016.

  1. 0710

    0710

    Joined:
    Jul 18, 2016
    Posts:
    2
    My hierarchy looks like this:
    • Manager
    • Camera
    • TileMap
      • Layer1
      • Layer2
    In my layers I have tiles with GameObjects attached to them, and those GameObjects have tags.
    I can't find those GameObjects using their tags from my Manager's Start(). Why?

    I know that if I call FindGameObjectsWithTag later on, I can find my objects. but I can't do it from Start().

    The only workaround I found so far was to attach my Manager to a Tile and add it to the TileMap. Otherwise my Manager's Start() is called before the Awake() of all my other GameObjects in the TileMap.

    Is there a way to make sure all GameObjects in my TileMap have been created before trying to find them by tag?

    Edit:
    A second workaround is to disable my manager by default and only enable it after the first Update() of my TileMap script.
     
    Last edited: Jul 19, 2016
  2. Xelnath

    Xelnath

    Joined:
    Jan 31, 2015
    Posts:
    402
    I believe it has to do with objects loading in the order they appear in the list.

    When something has children, it can find its children, because they will be loaded first. However, siblings at the scene level will be loaded in order.
     
  3. Jay-Pavlina

    Jay-Pavlina

    Joined:
    Feb 19, 2012
    Posts:
    195
    There is a method on BaseTile called StartUp. Override that method to get a callback when the game object is instantiated. You can use it to add them to your manager script when they're created.
     
  4. 0710

    0710

    Joined:
    Jul 18, 2016
    Posts:
    2
    Moving the manager to the bottom of the list doesn't help. I think everything in the hierarchy is loaded before all the TileMap GameObjects are created.


    Thanks. I'll try this out. Adding them to the manager instead of looking for them from the manager seems like a good idea.