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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Unable to destroy buttons and texts in canvas

Discussion in 'UGUI & TextMesh Pro' started by Giovarco, Apr 10, 2016.

  1. Giovarco

    Giovarco

    Joined:
    Mar 10, 2016
    Posts:
    45
    canvas is a reference to my main canvas.

    I did a log of bottoni and text lengh and everything seems ok (2 buttons and 2 texts in canvas).

    Code (CSharp):
    1.   public void CancellaBottoniUnita()
    2.     {
    3.  
    4.         // Non riesco a distruggere i bottoni e i testi
    5.         Button [] bottoni = canvas.GetComponentsInChildren<Button>();
    6.         Text[] nomi = canvas.GetComponentsInChildren<Text>();
    7.        
    8.         foreach(Button bottone in bottoni )
    9.         {
    10.             Destroy(bottone);
    11.         }
    12.  
    13.         foreach (Text nome in nomi)
    14.         {
    15.             Destroy(nome);
    16.         }
    17.  
    18.  
    19.  
    20.     }
     
  2. Saddamjit_Singh

    Saddamjit_Singh

    Joined:
    Dec 4, 2015
    Posts:
    22
    Take their reference as a game object and then destroy them by gameobject.setActive(false)