Resort prefab in a scroll view at runtime

Hi,

I have a list of prefabs that need to appear in a certain order when they are spawned.Each prefabs has a specific scriptable object that is assigned to them that gives them the intended data.

For that, I use the following functions, it sorts the scriptable objects in the intented order and then we spawn the prefabs and associate to each of them the scriptable object in the intented order.

 private void SortPlayerCardList()
    {
        _playerCardsScriptableObjectToSpawn.Sort(SortPlayerCardByOrderOfImportance);

    }

    private int SortPlayerCardByOrderOfImportance(ScriptableObjectPlayerCard a, ScriptableObjectPlayerCard b)
    {
        if (a.cardSortOrderNumber < b.cardSortOrderNumber)
        {
            return -1;
        }
        if (a.cardSortOrderNumber > b.cardSortOrderNumber)
        {
            return 1;
        }

        return 0;
    }

Now, my problem is that when the player selects certain game object, it can spawn new ones and therefore we need to make a new sort of the entire game objects but this time, it does not work.

I tried to create new function to resort the prefabs according to their sort order but the prefas contained in the scroll view are not moving.

So my question is the following : how to sort already spawned prefabs that are children of a scroll view ?

Thanks

You’re going to have to maintain a list of already spawned elements, sort those based on your criteria, and then set their index in the hierarchy to go from top to bottom.

1 Like

Thank you, works like a charm