Child-saving script
Hi, I have small problem with my script. There it is:
void OnDestroy() {
// I'll just save all children (bullets)
foreach(Transform child in transform) {
Debug.Log("Rescued: " + child.name);
child.parent = null;
}
}
I have object with children (ship with bullets). When ship is destroyed I'll save children and destroy them in other way. My code works at half. Really all my bullets are still alive when parent is killed, but colliders and scripts attached to them are disabled. It's a little strange behaviour and I have no idea what happend. Could anyone help?
I found other way in transform class - "DetachChildren" method, bit it also disable scripts and colliders for my bullets...
Answer by Kopczak1995 · Apr 01, 2016 at 09:07 AM
Ok, I found solution for this problem so I leave this for comunity:
transform.DetachChildren();
Destroy(transform);
I think problem was because before "OnDestroy" method something was done in Unity so my objects was disabled, but when I call DetachChildren();
direct before Destroy()
method - everything works fine.