Search Unity

Does component order matter?

Discussion in 'Editor & General Support' started by DaveyJJ, May 16, 2005.

  1. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    Does the order that an items components appear make any difference at all to the object or it's behaviour? Does putting, say, the FPS script ahead of the physics scripts make a difference? Or is the object instantiated as a whole entity with all things running (sort of)?
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Component order does not matter.
     
    Silvermax1975 likes this.
  3. zaomaohao

    zaomaohao

    Joined:
    Jan 3, 2015
    Posts:
    1
    I understand what you are saying but I think there should be more comments regarding the threat that was initially started so that the pool of thoughts is attracted. Regards.
     
    matt1101 likes this.
  4. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    With the exception of Image Effect components of course, but I'm just adding this just in case other readers get confused.
     
    parkerhg and matt1101 like this.
  5. dylinstoen

    dylinstoen

    Joined:
    Dec 19, 2018
    Posts:
    2
    90% of the time it doesn't matter however, As with scripts. The order to which the updates run depend on the order of the components top down. The top scripts update will run before the bottom exc... I found this out the hard way as i spent all day debugging my script just to find out the error was caused by the order of my scripts. If the order of your components matter you should change your code so thats not the case anymore as it just means you have bad code like me:)
     
  6. Nick62

    Nick62

    Joined:
    Jan 30, 2017
    Posts:
    11
    I have tested execution order. There is no dependency of script order on the gameobject with script execution order.

    Code (CSharp):
    1. public class A : MonoBehaviour {
    2.     private void Awake()
    3.     {
    4.         Debug.Log("=== A Awake ===");
    5.     }
    6.  
    7.     private void OnEnable()
    8.     {
    9.         Debug.Log("=== A OnEnable ===");
    10.     }
    11.  
    12.     private void Start()
    13.     {
    14.         Debug.Log("=== A Start ===");
    15.     }
    16.  
    17.     private void Update()
    18.     {
    19.         Debug.Log("=== A Update ===");
    20.     }
    21. }
    Code (CSharp):
    1. public class B : MonoBehaviour
    2. {
    3.     private void Awake()
    4.     {
    5.         Debug.Log("=== B Awake ===");
    6.     }
    7.  
    8.     private void OnEnable()
    9.     {
    10.         Debug.Log("=== B OnEnable ===");
    11.     }
    12.  
    13.     private void Start()
    14.     {
    15.         Debug.Log("=== B Start ===");
    16.     }
    17.  
    18.     private void Update()
    19.     {
    20.         Debug.Log("=== B Update ===");
    21.     }
    22. }

    upload_2019-7-20_12-30-55.png


    upload_2019-7-20_12-34-11.png
     
    leni8ec and Lingcod like this.
  7. Nick62

    Nick62

    Joined:
    Jan 30, 2017
    Posts:
    11
  8. Nick62

    Nick62

    Joined:
    Jan 30, 2017
    Posts:
    11
    With the separate GameObjects the same situation:

    upload_2019-7-20_12-44-48.png

    upload_2019-7-20_12-45-1.png
     
    Lingcod likes this.
  9. Nick62

    Nick62

    Joined:
    Jan 30, 2017
    Posts:
    11
    upload_2019-7-20_12-48-29.png

    upload_2019-7-20_12-47-30.png
     
    Lingcod likes this.
  10. Nick62

    Nick62

    Joined:
    Jan 30, 2017
    Posts:
    11
    upload_2019-7-20_12-49-52.png

    upload_2019-7-20_12-51-0.png
     
    Lingcod likes this.
  11. Silvermax1975

    Silvermax1975

    Joined:
    Jul 7, 2018
    Posts:
    1
    Then, how can you make the A script execute first?
     
  12. einWikinger

    einWikinger

    Joined:
    Jul 30, 2013
    Posts:
    97
    You can change execution order either in the project settings or by using the
    [DefaultExecutionOrder]
    attribute on the behaviour class.
     
  13. yaxamie

    yaxamie

    Joined:
    Mar 11, 2017
    Posts:
    8
    It's been a long time since this post was made but this comment shows up as a Google answer.

    I'm currently messing around with having two IUpdateSelectedHandler scripts on the same object and Component order seems to be used (rather than script execution order) for determining which script gets "Selected" first.

    It seems like this is no longer true in all niche cases.
     
  14. Max-VRAM

    Max-VRAM

    Joined:
    Jan 11, 2022
    Posts:
    3
    Component ordering is relevant when processing audio buffers using
    OnAudioFilterRead
    .

    I found a post showing a solution using the undocumented class,
    ComponentUtility
    , which has functions to move, copy, and paste components via script. I extended
    MonoBehaviour
    to make the functions easier to use:

    Code (CSharp):
    1. public static void MoveUp(this MonoBehaviour monoBehaviour)
    2. {
    3.     UnityEditorInternal.ComponentUtility.MoveComponentUp(monoBehaviour);
    4. }
    5.  
    6. public static void MoveDown(this MonoBehaviour monoBehaviour)
    7. {
    8.     UnityEditorInternal.ComponentUtility.MoveComponentDown(monoBehaviour);
    9. }
    10.  
    11. public static void Copy(this MonoBehaviour monoBehaviour)
    12. {
    13.     UnityEditorInternal.ComponentUtility.CopyComponent(monoBehaviour);
    14. }
    15.  
    16. public static void Paste(this MonoBehaviour monoBehaviour)
    17. {
    18.     UnityEditorInternal.ComponentUtility.PasteComponentValues(monoBehaviour);
    19. }
    20.  
    21. public static void PasteNew(this MonoBehaviour monoBehaviour, GameObject targetObject)
    22. {
    23.     UnityEditorInternal.ComponentUtility.PasteComponentAsNew(targetObject);
    24. }
    25.  
    26. public static void DuplicateOn(this MonoBehaviour monoBehaviour, GameObject targetObject)
    27. {
    28.     UnityEditorInternal.ComponentUtility.CopyComponent(monoBehaviour);
    29.     UnityEditorInternal.ComponentUtility.PasteComponentAsNew(targetObject);
    30. }
    Ref: https://discussions.unity.com/t/change-component-order-via-script/90871/3