Search Unity

Destroying Part of a prefab

Discussion in 'Scripting' started by WaelT, Sep 2, 2014.

  1. WaelT

    WaelT

    Joined:
    Jun 27, 2013
    Posts:
    5
    Is it possible to use C# script to destroy a part of a prefab?
    I have a prefab that is made from 3 cubes and in a certain condition I want to destroy one of those cube..
    Any suggestions?

    Thank you
     
  2. Limeoats

    Limeoats

    Joined:
    Aug 6, 2014
    Posts:
    104
    The GameObject that you have the script attached to can't access its children directly. That GameObject does, however, have a transform that can then reference the children.
    It would look something like this (untested but I think it should work):

    Code (CSharp):
    1. //The parent gameobject in the example below is called "prefab"
    2. foreach (Transform child in prefab.transform) {
    3.     //check the name of the child to get the one you want to delete
    4.     if (child.name == "name of child you want to delete") {
    5.         Destroy(child.gameObject);
    6.     }
    7. }
     
    Demoneid likes this.