Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Order of Components on Prefab isn't saved in Unity 5

Discussion in 'Editor & General Support' started by hassanselim0, Mar 5, 2015.

  1. hassanselim0

    hassanselim0

    Joined:
    Mar 5, 2015
    Posts:
    1
    So before upgrading to Unity 5 I had this item interaction system where you add components on an interactive game object to specify exactly what should happen when the player character interacts with that object (show dialog, switch sprite, give item, .. etc). The ordering of these components (actions) is very important in some cases (when an action delays the execution of the following actions, like with the dialog action).

    When I upgraded to Unity 5 I found this weird problem:
    When I press the play button, the ordering of the components sometimes changes, and this change is preserved when I stop and go back to editing. This problem doesn't seem to happen on normal (non-prefab) game objects, nor to the ordering of the components inside the prefab itself, it only happens to EXTRA components added after the Prefab is brought to the scene!

    I'm guessing this has to do with how prefabs and scenes are saved now in Unity 5, I found this line in the insanely large change log:
    So what about modifications to a prefab, where are they stored and how? It seems that the order of EXTRA components isn't saved!
    I even forced text serialization and did some diffs, and it seems that whatever reordering I do to these extra components, the files are exactly the same!

    I really hope they fix this bug, I don't want to add an "order" property to my action components, it can get messy and I want the order to be clear to the level designers without having to look at numbers!
     
    Last edited: Mar 5, 2015
  2. Le-nain

    Le-nain

    Joined:
    Nov 26, 2012
    Posts:
    62
    Sorry, I don't have any solution, I just want to add that this happens here aswell, and can indeed be pretty annoying. Hoping UT is having a look into this.
     
  3. OskarSwierad

    OskarSwierad

    Joined:
    Dec 17, 2014
    Posts:
    25
    I have exactly the same problem. All the extra components on a prefab get reordered after I press play.
    It's an annyoing bug, as the only workaround I can think of is to add them during game start through scripting.
     
  4. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    Hi,

    The order of components are not stored any where. It does not have anything to do with the fact that prefab instances are now stripped from the scene files.

    Lets have a look at the components that are part of the prefab first.

    Reordering the existing components of a prefab instances actually does not work. It only appears to work in play mode in the editor because of the way we store the temporary scene file when entering play mode. If you were to build a player the order of the components would be restored to the order saved in the prefab asset.

    If you are using the same prefab asset as template for multiple instance and don't need the order to be different between instances, just make sure your prefab asset has the components in the right order by setting the order up on one of the instances and clicking "Apply" in the inspector.

    If you are using the same Prefab asset as template for multiple instances but need to have different order of the components in each instance, you have to break the connection to the prefab asset. Select you prefab instance in the hierarchy and go to the menu "GameObject -> Break Prefab Instance"

    For added components you only have one option and that is to break the prefab connection.

    I don't remember specifically where the change was made, maybe in one of the patch releases or maybe in 5.1, but we have changed the Editor to always ask you to break the prefab connection when ever you start changing the order of the components on a prefab instance, exactly in order to make people aware of this issue.
     
    Last edited: May 6, 2015
    OskarSwierad likes this.
  5. OskarSwierad

    OskarSwierad

    Joined:
    Dec 17, 2014
    Posts:
    25
    Thanks for a detailed reply on the topic!
     
  6. WF_Bart

    WF_Bart

    Joined:
    Apr 16, 2014
    Posts:
    29
    So why do I get this problem even when I'm not changing the order of components that are part of the prefab? In my case I am adding components to a MODEL, and like the original poster, the order of the components is important!

    Losing references to the model file is a huge pain (if the model file gets updated, everything will be messed up!)
     
  7. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    @WF_Bart MODELS are prefabs. So the same rules applies to models.

    One solution could be to add components to your model using an asset post processor, I have not actually tried this so I cant guarantee that it will work
     
  8. sfjohansson

    sfjohansson

    Joined:
    Mar 12, 2013
    Posts:
    369
    Sorry for squeezing in here but it seemed related.. I just tried stacking a simple sequential state machine as mono behaviours and I noticed that if I have multiple instances of a type of component/class..the component order would break when I hit run..

    So is it not possible to control the order of components by arranging them in hierarchy on the game object?
     
  9. Deleted User

    Deleted User

    Guest

    If component order is crucial to your set-up, one solution would be to add a component to your GameObject that keeps tracks of the order of its components.
    Rough idea:

    Code (CSharp):
    1. public class ComponentOrderList : MonoBehaviour {
    2.     public Component[] components;
    3. }
    The user could drag & drop components in the order they should be in. If it's something you use a lot in your framework, you could invest the time in making a re-orderable list for it. Rotorz would be great for that or if you prefer you can use Unity's internal re-orderable list.
    Anyway, then all your class that rely on order would go through the above class to get to the next component in the list. Again, if it's something crucial to your framework, you could make all your classes inherit from a common base class that has a link to the list, kinda like the relationship between components & gameobjects.
     
  10. sfjohansson

    sfjohansson

    Joined:
    Mar 12, 2013
    Posts:
    369
    Hmm... a little sad...the order of components on a game object visually already represent a nice order that is very clear and intuitive..and adding another list where you have to arrange order feels like a case of "don't repeat yourself"

    I do have this easy editor script which is using Rotorz. I have to admit that editor scripts are not my strongest side and I was hoping to avoid any gotchas and potential can of worms that might come along by travelling down that route that in the end will become a project of it's own.

    In my specify case they would be a stack of different subclasses of an Action Base Class, with all different attributes..

    I wonder if it would be possible..to cache the "draw/visual inspector" order in a variable in the base class..then at least the execution will be correct even if the editor visuals get screwed up
     
  11. Deleted User

    Deleted User

    Guest

    I agree with you that it feels like it's violating DRY but technically you're not repeating yourself because the component order is something on the Unity side. That's how I'd look at it to make me feel better about implementing this system. :p