Parent in child OnDestroy() function

Suppose we have a hierarchy of empty game objects like this:

Parent
–Child1
–Child2

I have to access parent in OnDestroy() method of the children. However, transform.parent inside this function returns null. Is this some kind of bug? This moment is not highlighted in the documentation at all, so I am confused. Thanks in advance.

I tried this in both Unity 4.5, 4.6 beta. Also tried different script languages to no avail.

Noone can add anything related to this?

Cache tranform in variable

For example try this.

Transform parent;

void Awake(){
parent = transform.parent;
}

OnDestroy(){
parent…
}

Thank you for response. I’m familiar with the workaround you provided and I am using it in my codebase right now. However, I wanna know why the child game object doesn’t contain its parent transform inside OnDestroy() function. I want to comprehend this. I have a number of reasons to why caching the parent is not an optimal option in my case.

I’m tried doing like you and i get different result.

usingUnityEngine;
usingSystem.Collections;

publicclassNewSrc: MonoBehaviour {

//Usethisforinitialization
voidStart () {
Destroy (gameObject);
}

//Updateiscalledonceperframe
voidUpdate () {

}

voidOnDestroy(){
var test = transform.parent;
}
}

this script attach on child object and var test = parent inside OnDestroy - not null. Check your code or show source.

So you are saying that transform.parent in OnDestroy() is not null? Interesting, since I have tested it on 4.5 and 4.6 Unity versions and also on different PCs. Can you provide more details about your Unity version?

Oh I realised that I haven’t been destroying the child by calling Destroy, I actually deleted it directly from the hierarchy. In this case OnDestroy() does gets called, but transform.parent is null. I think this is why we have different results. Still, I need this script to function in editor.