Is it okay to reference a child's parent?

Hey all,

So the question here is, if I have a MonoBehaviour (A) that references another MonoBehaviour (B), is it okay for B to store a reference to A?

I am aware that unity doesn’t complain when and if the above happens, and it looks like unity’s own MonoBehaviours implement this (try doing Debug.Log(this.gameObject.gameObject.gameObject.name) and you’ll see what I mean). But I wonder if unity not complaining about this is just because it’s a very common thing that developers might do. Does it in fact impact performance and as a result is something that we should avoid?

This question comes from a PHP background, where referencing a parent object from a child object can cause problems…

Thanks in advance.

Debug.Log(this.gameObject.gameObject.gameObject.name);
is no different than

 Debug.Log(this.gameObject.name);

your version is saying: this gameobject, this gameobject, this gameobject, name. its redundant. gameobject inherits from object and so does component. this is why youre able to do gameObject.gameObject.gameObject. Components are always attached to gameobjects.

Referencing a parent from a child doesnt cause problems. If you think of child gameobjects in more of a folder structure it might be easier to understand. You could have any number of child objects referencing the same parent and its not an issue

So to answer your question…it is ok to have child monobehaviours store parent monobehaviours. You see it everywhere, many people do it, I have never heard or had any issues regarding this.