Need a bit of help in shuffling UI buttons and updating in in he Hierarchy

Hi guys. I’m a stuck on updating the game UI button in the Hierarchy but on the Inspector it’s already been shuffled

Image link if ever the image on the spoiler is pixalted

Image

As seen on the Image on the Inspector I was able to shuffle the buttons but I’m having problem in updating the buttons on the Hierarchy.

I’ve tried lots of ways and search the google. I can’t to make it work;
Tried using this and iterate from it. And other links that I can’t find or remember the title anymore.

Here’s the code I use to create the button in Awake()

    void CreateAllButtons()
    {
        for(int i = 0; i < numberOfButtonsToCreate; i++)
        {
            Button buttonInstance = Instantiate(buttonPrefab);
            buttonInstance.gameObject.name = "" + i.ToString();
            buttonInstance.GetComponentInChildren<Text>().text = i.ToString();
            buttonInstance.gameObject.transform.SetParent(buttonHolderPanel, false);

            numberButtons.Add(buttonInstance);
        }

        ShuffleButtons(numberButtons);

    }

    void ShuffleButtons(List<Button> buttonToShuffle)
    {
        List<Button> tempNumberButtons = new List<Button>(buttonToShuffle);

        for(int i = 0; i < buttonToShuffle.Count; i++)
        {
            int randomIndex = Random.Range(0, tempNumberButtons.Count);

            buttonToShuffle[i] = tempNumberButtons[randomIndex];
            tempNumberButtons.RemoveAt(randomIndex);
        }
    }

Thanks.

Do you have any UI layout groups? If not, and you’ve manually placed items, hierarchy won’t matter. And layout groups will only change during runtime (as far as I’ve seen)

Sorry forgot to mention I’ve used Grid Layout Group at the Main Game Panel

According to the image you’ve posted, the buttons aren’t actually changed in the hierarchy, only randomized in a script.
You need to actually change the hierarchy order.
I forget the exact method, but you could do something like

for(i int = 0; i < ButtonList.count; i++)
{
ButtonList[i].transform.SetSiblingIndex(i)
}

Where ButtonList is a list of your button objects in a randomized order.

Dude. That did the trick. Thank you Very much! :slight_smile:

Glad to be of service.