- Home /
Destroying childs of a specific Parent
Hi, i have a scene with six tables, these tables have 4 slots for "beers", when a beer is placed in one of these slots it is turned into its child, my question is: how can i destroy the beers of a specific table without affecting the other tables in the scene?
The closest i've managed to achieve is the following
public void ClearTable(){
GameObject go = GameObject.Find("mug(Clone)");
Destroy (go);
}
but that destroy the mugs of all tables. The Hierarchy is the following:
the mugs become child of one of the "BeerHolder's".
Sorry if my question is kinda confusing, i've have confused myself enough over this, and language barrier may be impeding me of expressing my question properly.
Thanks in advance for any answers.
Answer by VesuvianPrime · Dec 12, 2014 at 10:42 PM
If you find yourself using GameObject.Find to do things you're probably doing something wrong.
Instead of trying to exploit GameObject.Find to do this, lets make a more formal hierarchy.
I would approach the problem like this:
public class Tavern : MonoBehaviour
{
public List<Table> tables;
}
public class Table : MonoBehaviour
{
public List<Beer> beers;
public void Clear()
{
for (int index=0; index < beers.Count; index++)
GameObject.Destroy(beers[index]);
beers.Clear();
}
}
public class Beer : MonoBehaviour
{
}
Your answer
Follow this Question
Related Questions
Script that mimics a child object behavior. 1 Answer
Does transform.root work when a parent is made the child of another object through script? 1 Answer
Getting the topmost parent 1 Answer
Convert RectTransform rect to screen space 12 Answers
I want to keep the rotation and the position of a parent object when i give it a child?? 0 Answers