Hi guys,
I am quite new in programming but I wish to script in javascript to destroy any child by the parent.
Something like
Destory(IamParent.destroyChild) → any child spawn by IamParent will be destroy
Is there any script can destroy the specific parent child?
Because the child is always random object so I wish to destroy the child when something is trigger.
Thank in advance
To destroy a random child, use something like
var randomValue = Random.value;
var i : int = 1;
for(var trans : Transform in transform)
{
if(i / transform.childCount > randomValue)
{
Destroy(trans.gameObject);
break;
}
}
Although, that doesn't destroy a specific child, only a random one. To destroy a specific child, you would need to keep manual references to each one of them, and then destroy the one you actually wanted to destroy.
Thank syclamoth as your script reference, I have success to destroy the child in the parent
for(var child : Transform in IamParent)
{
Destroy(child.gameObject);
}