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
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Tilemap] Bug with tile gameobject instantiation?

Discussion in '2D Experimental Preview' started by castor76, Feb 25, 2020.

  1. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    I have set up my gameobject to be instantiated for the base tile, but it does not instantiate it on painting on the tilemap. When you enter play mode, it instantiate when painting or hovering over the exiting tile.

    I have set all my flags to none, so it isn't the cause of "instantiate only runtime". The gameobjects does not instantiate unless I manually paint again during play mode or hover over them.

    This seems strange and must be a bug? Or have I missed something?

    The tile I am painting is just base tile class and I have manually assigned the gameobject by using debug mode in the inspector.
     
  2. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Could share an image of the Inspector of GameObject you are using to be instantiated and the Tile? Other details such as the version of the Unity Editor would be helpful as well.

    Alternatively, you can file a bug with the Unity Bug Reporter and post the case number here, we wouuld be happy to investigate there as well!
     
  3. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    upload_2020-2-28_16-51-37.png

    Here is image of the gameobject I want to instantiate. Really simple gameobject with just a simple boxcollider.

    This does not get instantiated... however, on testing with some "other" gameobject such as simple sprite, it did work.

    This confuses me... is there any restrictions on what kinds of gameobject it can instantiate or not?
     
  4. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    Coming to think about it.. maybe it is instantiating it.. but just not visible... even with the gameobject with sprite renderer on it seems to work, there are no gameobjects actually seen in the hierarchy. Are they all hidden by default?

    I can confirm that painting the same brush during runtime, does indeed instantiate the game objects and can be seen in the hierarchy, but not during edit mode. The tile flag for "instantiate only during runtime" is not set. During edit mode, it seems to instantiate the gameobject but they do not appear on the hierarchy.

    I am using the latest Unity 2019.3.3f1
     
    Last edited: Feb 28, 2020
  5. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Yes, they are hidden. This is to prevent duplicates from being saved to the Scene, especially in Edit Mode, otherwise each time you load the Scene and save it, you will get more and more added to the Scene.
     
  6. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    But then during runtime, it needs to instantiate? Problem is it does not instantiate even during runtime.

    The painted tile with game object to instantiate should do so at least during run time. But they are not.. they only instantiate when i hover over them using paint brush.
     
    Kwwa likes this.
  7. Kwwa

    Kwwa

    Joined:
    Jun 1, 2013
    Posts:
    3
    Edit2: This solution will instantiate another gameobject on playmode and when instantiated with the tile palette. However, this does not happen with the DontSave flag on.

    For anybody that struggled with this, add the following lines of code inside the StartUp() function on the Scriptable Tile.

    Code (CSharp):
    1.  
    2. go.hideFlags &= ~HideFlags.HideInHierarchy;
    3. go.hideFlags &= ~HideFlags.NotEditable;
    4. go.hideFlags &= ~HideFlags.DontSave;
    5. #if UNITY_EDITOR
    6.   try { EditorApplication.DirtyHierarchyWindowSorting(); } catch { }
    7. #endif
    8.  

    This code assumes you have already defined your tile prefab on the GetTileData() function.

    Refactorings accepted, this is a quick fix. Probably just setting go.hideFlags = HideFlags.None would do the trick.

    Edit:
    I meant, setting the hide flags to none and manually updating the hierarchy window should do the trick.
     
    Last edited: Apr 27, 2020
  8. Waz

    Waz

    Joined:
    May 1, 2010
    Posts:
    287
    While I'm seeing them instantiated just fine, they seem to be instantiated some time after all Start() methods are called and before the first Update() call, which makes it impossible to do anything during the setup of the tilemap gameobjects.
     
    iSinner likes this.
  9. iSinner

    iSinner

    Joined:
    Dec 5, 2013
    Posts:
    201
    I noticed the same, and i have an issue setting up the children of a tilemap due to this behavior.

    I have a script "A" on the tilemap object, which is the parent of all tiles in that tilemap. Some tiles are prefab instances with a scrip "B" on them. In editor, script A during update obtains all scripts B in children and applies some modifications to prefab instances that have script B on them, these changes vanish when i enter play mode, and i suspect this happens because the tiles get re-instantiated sometime after the start method.

    So my question is, when i enter play mode, since i can't keep the changes in edit time, how would i one-time-update my prefab instances with the script B on them that are instantiated after the start method under the tilemap object, w/o using the update loop and some bools? i don't really want to have the update loop run on every tile, when it isn't actually needed.
     
  10. SXtheOne

    SXtheOne

    Joined:
    Sep 5, 2018
    Posts:
    21
    @ChuanXin
    I'm trying to bake a lightmap by using these gameobjects instantiated by tiles but can't because of these hideflags. Can I disable the hideflags so I can build the lightmap?

    If it is not possible I probably need to scrap the gameobjects and write a separate script which instantiates them so it is kind-of important for me.Thanks!
     
  11. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Unfortunately, you would have to manually disable the hide flags through a script before building your lightmap, and enable them after to prevent them from being saved to your scene.
     
    SXtheOne likes this.
  12. SXtheOne

    SXtheOne

    Joined:
    Sep 5, 2018
    Posts:
    21
    I can do that no problem but where should I search for the instantiated gameobjects? I mean - probably because of the hide flags - the objects are not seeable.
    Are they in the same place where they can be found runtime and can be found with a transform.GetChild(index)?
     
  13. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    They should be children of the Transform which has the Tilemap component which stores the Tiles that have instantiated them, iterating using transform.GetChild should work.
     
    SXtheOne likes this.