Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
8
Question by Jordii · Oct 27, 2010 at 03:34 PM · componentorder

Changing the order of Components

In some cases the order affects the end result. For instance when stacking a blur on a noise, or the other way around. Does Unity have an ability to change the order? I've been looking around in the docs and Google, but couldn't find it. I was even a bit surprised that I couldn't find anything :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

9 Replies

· Add your reply
  • Sort: 
avatar image
19
Best Answer

Answer by pigzlz · Nov 13, 2012 at 11:15 PM

Since Unity 4, you can move components up and down by right-clicking on them (or clicking on the small gear in the right corner) and selecting the appropriate option in the context menu that is popping up.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image chirpytime · Jan 31, 2013 at 02:30 PM 2
Share

Can we do this programmatically? Like sorting components by a property value?

avatar image Eran-Yaacobi · Nov 10, 2013 at 09:45 PM 9
Share

There is an option (only in the editor), though it is not documented:

 UnityEditorInternal.ComponentUtility.$$anonymous$$oveComponentDown(Component);
 UnityEditorInternal.ComponentUtility.$$anonymous$$oveComponentUp(Component);

There are also other functions in there, for copying and pasting components.

avatar image IgorAherne Eran-Yaacobi · Apr 16, 2017 at 08:09 PM 0
Share

wow, thank you @EranYaacobi

avatar image Marzoa · Nov 05, 2014 at 06:27 PM 0
Share

Thanks, this what useful for me, though it is a pain in the ass when you have dozens of components and need to move the last one among the firsts. Now, that's usability, indeed...

avatar image
5

Answer by dyankov · Jun 09, 2014 at 09:51 AM

New plugin on the Asset Store, doing just what you need with a drag and drop interface:

https://www.assetstore.unity3d.com/en/#!/content/18393

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by Eran-Yaacobi · Oct 11, 2015 at 05:22 PM

Posting this as an answer (instead of as a reply to a somewhat different answer), along with an example for an editor script that can be used to re-order components by type.

There is an option to sort components programmatically (only in the editor), though it is not documented:

UnityEditorInternal.ComponentUtility.MoveComponentDown(Component); UnityEditorInternal.ComponentUtility.MoveComponentUp(Component);

There are also other functions in there, for copying and pasting components.

Here is an example for a useful script which provides a menu item (shortcut alt+ctrl+a) that sorts the components in the currently selected game-object according to type:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using UnityEngine;
 using UnityEditor;
 using UnityEngine.Networking;
 
 public class ComponentsSorter : ScriptableObject
 {
     private class ComponentComparer : IComparer<Component>
     {
         private static readonly Type[] TypesOrder =
         {
             // Transform is always first (though that doesn't really matter, as we can't 
             // move it anyway).
             typeof (Transform),
 
             // Add your types here in the order you want them to be in the inspector.
             typeof (MeshFilter),
             typeof (MeshRenderer),
             typeof (SpriteRenderer),
             typeof (Rigidbody2D),
             typeof (Collider2D),
         };
 
         private Int32 GetIndex(Component Component)
         {
             var Type = Component.GetType();
 
             Type BestMatch = typeof(UnityEngine.Object);
             var BestIndex = Int32.MaxValue;
             for (int Index = 0; Index < TypesOrder.Length; Index++)
             {
                 // If we found the exact type in the list, then this is the right index.
                 var TypeOrder = TypesOrder[Index];
                 if (Type == TypeOrder)
                     return Index;
 
                 // If we found a parent, then we switch to its place if it is more
                 // "recent" (in the inheritance tree) than previously found parents.
                 if (Type.IsSubclassOf(TypeOrder))
                 {
                     if (TypeOrder.IsSubclassOf(BestMatch))
                     {
                         BestMatch = TypeOrder;
                         BestIndex = Index;
                     }
                 }
             }
 
             return BestIndex;
         }
 
         public int Compare(Component First, Component Second)
         {
             return Comparer<Int32>.Default.Compare(GetIndex(First), GetIndex(Second));
         }
     }
 
     [MenuItem("Edit/Sort Components %&a")]
     private static void SortComponents()
     {
         var GameObject = Selection.activeGameObject;
         var SortedComponents = GameObject.GetComponents<Component>()
             .Where(Component => Component.GetType() != typeof(Transform)).ToList();
         SortedComponents.Sort(new ComponentComparer());
 
         for (var Index = 0; Index < SortedComponents.Count; Index++)
         {
             var SortedComponent = SortedComponents[Index];
             var Components = GameObject.GetComponents<Component>()
                 .Where(Component => Component.GetType() != typeof (Transform)).ToList();
             var CurrentIndex = Components.IndexOf(SortedComponent);
             if (CurrentIndex < Index)
             {
                 for (var MoveIndex = CurrentIndex; MoveIndex < Index; MoveIndex++)
                     UnityEditorInternal.ComponentUtility.MoveComponentDown(SortedComponent);
             }
             else
             {
                 for (var MoveIndex = CurrentIndex; MoveIndex > Index; MoveIndex--)
                     UnityEditorInternal.ComponentUtility.MoveComponentUp(SortedComponent);
             }
         }
     }
 }

The code is a little ugly, but it works. Just add it under an 'Editor' folder and it should appear under the 'Edit' menu.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by ransomink · Jan 15, 2018 at 02:02 AM

Now, you can simply click & drag a component to change its order...

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by CiberX15 · Oct 11, 2015 at 04:25 PM

Actually You can do this now. I'm not sure about previous Unity versions but in Unity 5.2 you can right click the component and select "Move Up" or "Move Down" to move the component wherever you need it.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Component order on a game object changed on unity editor restart 0 Answers

My components are disappears 1 Answer

Is MovieTexture works on Linux Standalone app ? 1 Answer

Disable a C# script from another C# script. 1 Answer

Enabling and Disabling a Canvas 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges