- Home /
Destroying Children Individually (Horrible..)
Hey guys this is my first post asking for awnsers and i am wondering how i would go about picking a random child from a parent of 125 children and destroying ONE alone, not all at once.
Here is my current code
for(var i = 0; i < 4;i++)
{
var AmountDestroy = Random.Range(1,100); // The amount of children to be destroyed
var createdchunk = Instantiate(PreChunk,Vector3(1*i,1*i,1*i), Quaternion.identity);
for (var del = 0; del < AmountDestroy; del++)
{
// Need the Child destruction here
}
Thank you to whoever is willing to help
Answer by Bunny83 · Jun 08, 2012 at 02:51 PM
The easiest way is to use GetChild together with childCount. GetChild is no longer described in the documentation, but as afaik it's still there.
for(var i = 0; i < 4;i++)
{
var AmountDestroy = Random.Range(1,100); // The amount of children to be destroyed
var createdchunk = Instantiate(PreChunk,Vector3(1*i,1*i,1*i), Quaternion.identity);
for (var del = 0; del < AmountDestroy; del++)
{
var index = Random.Range(0,tranform.childCount);
Destroy(transform.GetChild(index).gameObject);
}
}
Answer by tet2brick · Jun 08, 2012 at 02:15 PM
Can you assign a unique name with a number in it to your children?
If yes, I'll go with something like that :
for(var i = 0; i < 4;i++)
{
var AmountDestroy = Random.Range(1,100); // The amount of children to be destroyed
var createdchunk = Instantiate(PreChunk,Vector3(1*i,1*i,1*i), Quaternion.identity);
for (var del = 0; del < AmountDestroy; del++)
{
var randChildNr=Random.Range(1,125);
destroy(transform.find("sometexthere"+randChildNr))
}
This is a bad example. What if transform.Find can't find the object? You will get a null-reference and Destroy will throw a null-reference exception.
Also Find has a capital F as well as Destroy has a capital D and you're missing a semicolon at the end ;)
Your answer
![](https://koobas.hobune.stream/wayback/20220613072529im_/https://answers.unity.com/themes/thub/images/avi.jpg)