Search Unity

Bug Scene hierarchy objects mess up after playing

Discussion in 'Editor & General Support' started by AlexVillalba, Mar 19, 2022.

  1. AlexVillalba

    AlexVillalba

    Joined:
    Feb 7, 2017
    Posts:
    346
    Hi everybody, the issue is simple: I use some game objects in the scene hierarchy as separators (for example, one called LIGHTS, and then I put all the lights below as siblings not children). When I press Play, then Stop, many objects appear in a different order in the hierarchy and I have to tidy up everything again.

    Is this a bug? Is it already fixed in next versions?

    Unity version: 2020.3.4.

    Thanks in advance.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    No, not a bug. Since about 2014 the order of root top level objects became explicitly undefined.

    There was a brief window when there was a "sort alphabetically" tickbox but I think even that is gone.

    You can put them as children of another objects and they will maintain their order after that point.

    Once they are children you can also use the sibling index methods on the Transform to move them around in code.
     
  3. AlexVillalba

    AlexVillalba

    Joined:
    Feb 7, 2017
    Posts:
    346
    Thank you. That's precisely what I don't want to do, I try to keep the hierarchy as plain as possible to avoid transformation inheritance (performance loss).
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Oh my lord, what a waste of effort.

    Make your hierarchy however you like. It will almost certainly NEVER be the hierarchy that impacts your performance.

    DO NOT OPTIMIZE "JUST BECAUSE..." If you don't have a problem, DO NOT OPTIMIZE!

    If you DO have a problem, there is only ONE way to find out. Always start by using the profiler:

    Window -> Analysis -> Profiler

    Failure to use the profiler first means you're just guessing, making a mess of your code for no good reason.

    Not only that but performance on platform A will likely be completely different than platform B. Test on the platform(s) that you care about, and test to the extent that it is worth your effort, and no more.

    https://forum.unity.com/threads/is-...ng-square-roots-in-2021.1111063/#post-7148770

    Remember that optimized code is ALWAYS harder to work with and more brittle, making subsequent feature development difficult or impossible, or incurring massive technical debt on future development.

    Notes on optimizing UnityEngine.UI setups:

    https://forum.unity.com/threads/how...form-data-into-an-array.1134520/#post-7289413

    At a minimum you want to clearly understand what performance issues you are having:

    - running too slowly?
    - loading too slowly?
    - using too much runtime memory?
    - final bundle too large?
    - too much network traffic?
    - something else?

    If you are unable to engage the profiler, then your next solution is gross guessing changes, such as "reimport all textures as 32x32 tiny textures" or "replace some complex 3D objects with cubes/capsules" to try and figure out what is bogging you down.

    Each experiment you do may give you intel about what is causing the performance issue that you identified. More importantly let you eliminate candidates for optimization. For instance if you swap out your biggest textures with 32x32 stamps and you STILL have a problem, you may be able to eliminate textures as an issue and move onto something else.

    This sort of speculative optimization assumes you're properly using source control so it takes one click to revert to the way your project was before if there is no improvement, while carefully making notes about what you have tried and more importantly what results it has had.
     
  5. AlexVillalba

    AlexVillalba

    Joined:
    Feb 7, 2017
    Posts:
    346
    Thank you for such a long post. I already know about the dangers of premature optimization and microoptimization. The thing here is different: For me, using the editor, the effort when putting an object at A or at B is the same, but putting it at B has a small performance cost, almost negligible if you want. If I could choose, I would always choose A. And so would do you too. And this is completely aside from the obvious need for profiling the game every now and then. If that option, avoiding that tiny performance cost, is not possible, then ok. I just was wondering whether it is or not. You confirm it is the normal behaviour so I will adapt.