Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resort prefab in a scroll view at runtime

Discussion in 'Scripting' started by jdg23, Sep 6, 2022.

  1. jdg23

    jdg23

    Joined:
    Feb 10, 2020
    Posts:
    16
    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.

    Code (CSharp):
    1.  private void SortPlayerCardList()
    2.     {
    3.         _playerCardsScriptableObjectToSpawn.Sort(SortPlayerCardByOrderOfImportance);
    4.  
    5.     }
    6.  
    7.     private int SortPlayerCardByOrderOfImportance(ScriptableObjectPlayerCard a, ScriptableObjectPlayerCard b)
    8.     {
    9.         if (a.cardSortOrderNumber < b.cardSortOrderNumber)
    10.         {
    11.             return -1;
    12.         }
    13.         if (a.cardSortOrderNumber > b.cardSortOrderNumber)
    14.         {
    15.             return 1;
    16.         }
    17.  
    18.         return 0;
    19.     }
    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
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    jdg23 likes this.
  3. jdg23

    jdg23

    Joined:
    Feb 10, 2020
    Posts:
    16
    Thank you, works like a charm