Search Unity

Prefab instantiation bug in 2018.3 (maybe sooner): Children get sorted alphabetically

Discussion in 'Prefabs' started by Sharlatan, Dec 20, 2018.

  1. Sharlatan

    Sharlatan

    Joined:
    Jul 23, 2013
    Posts:
    104
    Can anyone confirm this? In Unity 2018.3 (maybe sooner, I haven't used that part of my codes in the last few months, so I'm not sure if it still was okay in e.g. 2018.2), if I instantiate a prefab by code; e.g. something like this:
    Code (CSharp):
    1. var menuPrefab = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject));
    2. var menuInstance = (GameObject)Object.Instantiate(menuPrefab);
    , all the instantiated prefab's children get sorted automatically, even if they are not sorted that way in the prefab itself.

    If I drag the prefab into the scene via editor, this does not happen and the original element order remains, as it should be.

    In a lot of cases, this doesn't matter at all. But if your prefab contains UI elements, where sort order, order in layout groups, etc. are all based on the order of the objects in the hierarchy, this matters a great deal
    .

    It's not a problem for all my other prefabs but I handle all my menus as prefabs and instantiate them when needed and about 80% of my menu prefabs aren't usable anymore.

    A temporary workaround for this is by renaming all objects so they are in the right order when sorted alphabetically. But that's very cumbersome because you have to rename objects every time you want to move them around, you'll always have to leave some "alphabetical ordering space" out between two objects because, else, if you e.g name your objects 01*,02*,03*, etc and later on want to place an object between 01* and 02*, you'll have to rename all objects from and including 02*.

    Maybe it's a bit hard to explain/understand by text. Here's a graphical representation of one of the manifestations of the problem:

    This is how the order looks in the prefab or when I drag the prefab into the scene:


    This corresponds to the following menu:



    However, if I instantiate the prefab via code, all objects on the same hierarchy level are ordered alphabetically and the the order then looks like this:


    Which messes up the menu:


    But as said, there are also other problems with this. E.g. it affects the layering. If you have a big panel as a background and a small foreground panel displaying the content, you would order the background panel above/before the the foreground panel in your prefab. However, if those two panels switch places when ordered alphabetically and the background panel now is below/after the foreground panel, the background panel will now obscure the foreground panel.

    Is it just me? Am I doing something wrong? Or is this a bug? If so, is it already known (can't imagine no one noticed) or should I report it?

    Thanks a lot and have a nice day!

    @runevision From what I can tell from the forum posts in the prefab section, you are one of the Unity "prefab guys". Maybe you could chime in if this is intended or not and if not, if that's already a known problem or if I should create a bug? Thank you very much!
     
    Last edited: Dec 20, 2018
  2. bretternst

    bretternst

    Joined:
    Apr 8, 2017
    Posts:
    5
    That would be a serious bug for me too. Not sure if this helps you or not, but I instantiated a prefab using the same functions as in your code snippet and all objects retained the order they had in the prefab editor regardless of name. To narrow down the issue, have you tried it in a fresh project?
     
  3. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    437
    @Sharlatan

    Looking picture 2 of you hierarchy it seems like you have alphanumeric sorting enabled
     
  4. Sharlatan

    Sharlatan

    Joined:
    Jul 23, 2013
    Posts:
    104
    @SteenLund
    Thanks for the tip! It turned out to be something else though.
    I was using a 3rd party custom asset importer and created some prefabs in a hook method there. Turns out, it was the importer that somehow reordered my prefabs after that in the importing process.

    Should have made a simple example and/or investigate more, to really locate the problem.
    The only excuse that I have is, that I had been working for about 14 hours at that point in time and my thinking really started to get muddy. But should have stuck to the basics anyway and make the most simple example I could, sorry!

    I'll be more thorough next time. Sorry for the false alarm!
     
    Last edited: Dec 21, 2018
  5. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    437
    I am happy you found the issue
     
  6. Seanba

    Seanba

    Joined:
    Nov 17, 2012
    Posts:
    29
    Hello there. This is not a solved issue yet. There appears to be a bug with Unity's scripted importers where instantiated prefabs that are added to an imported object do not have their object hierarchy preserved.

    I have submitted a bug report on this issue here. Thanks.
     
  7. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    437
    @Seanba

    Ah ok, well ScriptedImporter is something else and I think it does so on purpose but we can have a chat with the ScriptedImporter devs about this issue.
     
  8. Sharlatan

    Sharlatan

    Joined:
    Jul 23, 2013
    Posts:
    104
    Since this doesn't seem to be solved after all, I took the liberty to remove the "[solved]" tag, I hope that's okay.

    @SteenLund Could you elaborate on why this could be/is probably on purpose? And would it be possible to update this thread with whatever came out of the discussion with the ScriptedImporter devs? Thank you very much!