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 /
avatar image
1
Question by tigertrussell · Mar 21, 2015 at 04:32 PM · uilayout

Re-order Horizontal Layout Group at Runtime?

Hi all,

Does anyone know how to re-order the children of a Horizontal Layout Group at Runtime?

Essentially I want to allow users to re-order buttons which are contained in a horizontal layout group.

Thanks for your time!

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

3 Replies

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

Answer by tigertrussell · Mar 21, 2015 at 04:53 PM

Transform.SetSiblingIndex(int) and Transform.GetSiblingIndex()

Comment
Add comment · Show 2 · 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 WytchWare · Oct 19, 2017 at 04:17 PM 0
Share

Wanted to add a comment in case others stumble upon my same issue.

This will not work if you're trying to move prefab objects around (accidentally) and it will fail without warning. Remember to instantiate your prefabs before trying this!

avatar image diekeure · Dec 17, 2020 at 11:33 AM 0
Share

Thanks! Exactly what I was needing today.

avatar image
1

Answer by BUWbrean · Jan 04, 2018 at 12:48 PM

Thank you very much for sharing your finding. I had to write a small script that ensures the order because every time I run my code the order changes (I think this might be a bug in Unity)

 using System.Collections.Generic;
 using UnityEngine;
 
 public class ForceButtonOrder : MonoBehaviour {
     [Tooltip("list of button names")]
     public List<string> order;
 
     // Use this for initialization
     void Start () {
         for (int i = 0; i < order.Count; i++)
         {
             transform.Find(order[i]).SetSiblingIndex(i);
         }
     }
 }
 
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 Fattie · Oct 08, 2020 at 11:59 AM

Yes, it's a ridiculous Unity bug.



Even if you are NOT reordering them at RUNTIME, horizontal layout groups sometimes screw the order of elements - so, they get changed from the order seen in the scene in the Editor.



In this simple example I had to just force all elements (there were four) to bne in the right place, doing so at Start() time seems to work. Nice spot by @BUWbrean



 using UnityEngine;
 
 public class BizarreGroupFixer : MonoBehaviour
 {
     // fix absurd Unity bug, which screws the ordering of
     // horizontal groups (perhaps if one is a prefab?)
 
     public Transform a;
     public Transform b;
     public Transform c;
     public Transform d;
     
     void Start()
     {
         a.SetSiblingIndex(0);
         b.SetSiblingIndex(1);
         c.SetSiblingIndex(2);
         d.SetSiblingIndex(3);
     }
 }

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

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

24 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Determining if Horizontal Layout Group has room for an element. 0 Answers

How to listen for UI Resize Events 3 Answers

OnPointerExit Not Being Triggered 1 Answer

New UI: Expandable button. How to implement ? 0 Answers

Unity5 new UI render problem on Android 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