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

destroying childobject of a gameobject's

Discussion in 'Scripting' started by verderveremem, Jun 17, 2014.

  1. verderveremem

    verderveremem

    Joined:
    May 11, 2014
    Posts:
    52
    how can i destroy childobject without using GameObject.Find method. I cant use Start() because this gameobcets are prefabs and its coming random seconds
     
  2. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    You can get the child through the transform. The question is, which child do you want to destroy? All children? Only ones with a certain tag? Only GameObject with ParticleSystems attached?
    Code (csharp):
    1. Destroy(transform.GetChild(0).gameObject);
     
  3. verderveremem

    verderveremem

    Joined:
    May 11, 2014
    Posts:
    52
    the main gameobject has a one childobject. So i want to destroy only one child object(empty object) :)
     
  4. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Then that code should work just fine!
    Code (csharp):
    1. // Check if we have a child to destroy.
    2. if (transform.childCount > 0) {
    3.   Destroy(transform.GetChild(0).gameObject);
    4. }