Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Clearing ScrollView

Discussion in 'General Discussion' started by MachairaJP, Nov 12, 2021.

  1. MachairaJP

    MachairaJP

    Joined:
    Jan 31, 2014
    Posts:
    30
    This didn't really seem to fit in any of the UI forums, so apologies if this doesn't belong here.

    I thought this would be a simple thing, but for some reason I can't seem to get this. I'm populating a scrollview that I'm using for multiple leaderboards with instances of a prefab, but I can't seem to clear the items when changing from one leaderboard to another. Since there could be any number of items in each leaderboard, clearing the scrollview is the cleanest way to go IMO.

    I've tried keeping the items in a List in a script attached to the scrollview, but destroying the items in the list doesn't destroy them in the scrollview. I can't seem to come up with a clean way to drill down into the children in the scrollview. None of the GetComponent methods work for instances of a prefab that I've seen.

    I'm probably just missing something really simple and overthinking it. :( Any help would be appreciated.
     
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,554
    First you should be able to just disable items. If it is disbaled, it does not participate in layout calculation.

    Second, when destroying, you need to destroy gameobject of the item and not a component of UI control.

    That's assuming you're using uGUI.
     
  3. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    974
    I have this thing I use:

    Code (CSharp):
    1. public static void ClearChildren(this Transform t)
    2. {
    3.     var children = t.Cast<Transform>().ToArray();
    4.  
    5.     foreach (var child in children)
    6.     {
    7.         Object.DestroyImmediate(child.gameObject);
    8.     }
    9. }
    Then you can just do scrollViewTransform.ClearChildren();
     
    Silverhook likes this.
  4. MachairaJP

    MachairaJP

    Joined:
    Jan 31, 2014
    Posts:
    30
    Disabling items isn't a good solution as I'd end up with 100s of unused items, clogging up the control. It would be more work to change the prefab instances than it's worth.

    I'm trying to destroy the prefab instances, which are gameobjects
     
  5. MachairaJP

    MachairaJP

    Joined:
    Jan 31, 2014
    Posts:
    30
    So it turns out I'm just stupid. :) I wasn't using the container's transform. This is what happens when you're using multiple game engines at the same time. :( The previous post provided the clear, although it doesn't work as written.
     
  6. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    974
    Ah, it is extension method. You'd need to put it in extension class!

    Full code would be:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Linq;
    3.  
    4. public static class MyExtensionClass
    5. {
    6.     public static void ClearChildren(this Transform t)
    7.     {
    8.         var children = t.Cast<Transform>().ToArray();
    9.  
    10.         foreach (var child in children)
    11.         {
    12.             Object.DestroyImmediate(child.gameObject);
    13.         }
    14.     }
    15. }
     
    Last edited: Nov 15, 2021