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

empty the GridLayoutGroup

Discussion in 'Scripting' started by BatmanDeveloper, Jul 24, 2017.

  1. BatmanDeveloper

    BatmanDeveloper

    Joined:
    Jul 1, 2017
    Posts:
    54
    Hi

    I am trying empty my GridLayoutGroup before show my buttons into GridLayoutGroup?

    My code:

    Code (CSharp):
    1.  
    2. foreach (enemy en in xnList)
    3. {
    4.   //First Clean
    5.   //<<<<=====
    6.  
    7.   //Later Show the Results Here
    8.  
    9.   GameObject enemyList = Instantiate (myButton) as GameObject;
    10.   enemyList.transform.SetParent (myPanel.transform, false);
    11. }
    12.  
    Thank You
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    I think GridLayoutGroup considers the transform children for what it displays.

    You can iterate the transforms and destroy them directly:

    Code (csharp):
    1.  for (int i = 0; i < myGrid.transform.childCount; i++)
    2.  {
    3.   Destroy( myGrid.transform.GetChild(i).gameObject);
    4.  }
    where myGrid is a reference to your GridLayoutGroup.

    Then you repopulate them with new bits that you create/load, by using transform.SetParent( myGrid.transform);
     
  3. BatmanDeveloper

    BatmanDeveloper

    Joined:
    Jul 1, 2017
    Posts:
    54
    First, Thank You my friend. so, I am using a Foreach. I am confused.

    Code (CSharp):
    1.  
    2. foreach (XmlNode xn in xnList)
    3. {
    4.  
    5. }
    6.  
    Thank You