Why is a parent object accessed through the transform?

I’ve been having a lot of trouble trying to understand this or find some good documentation for it (probably because I am having trouble asking the right questions) but my first question is the one stated in the title.

For example, I have a prefab, and that prefab consists of two GameObjects… one parent and one child.

Now when I am coding a script on the child element (of the prefab mentioned above) I got to the point where I needed to look at the Network View component on the parent GameObject. Perhaps what I was trying to do with the NetworkView is incorrect but I am just experimenting and learning… however before I could even get that far I first needed to get the parent GameObject before accessing its components.

I figured that would be easy enough and that there would be a parent GameObject associated to each GameObject which I could access, but after spending quite a while looking around, I found out that I need to get the parent through the transform. That is very strange to me. In my mind the transform just has some physical traits of the object like position and rotation, so why would I have to ask it about who the parent is. It feels like the transform has data that makes sense to associate with the GameObject. To me that doesn’t make sense, so if someone could clarify why it is like that I would appreciate it.

It has been causing me problems with getting the actual parent, and if you’d like to help me out even more than explaining the above, it would be great to figure out how to actually get the parent GameObject. I did transform.parent.gameObject (in the script which is on the child object) but it ends up returning a transform instead of a GameObject which confuses me further. transform.parent and transform.parent.gameObject both seem to return transforms, when I would expect the second to return the GameObject.

I must be missing some very obvious things because to me it seems like transforms are just used in place of GameObjects sometimes and they contain data that I would think the GameObject should have instead.

Sorry for the long question, and I’m sure this is very basic… but I am honestly having a lot of trouble looking this up and understanding it all.

Transforms are the heart of the parent/child relationships. Transforms store position, rotation and scale, and those values you see in the inspector for position, rotation, and scale are relative to the immediate parent. When I write a script to spin a propeller for a plane for example, I can spin it on it’s local ‘y’ axis, translate it to the front of the plane, and rotate it facing forward. Once set, the child does not have to worry about the orientation of the plane. It does it actions locally and everything works. Think how hard doing a proper rotation of the propeller would be without a parent/child relationship.

Unity uses a Component/Object model, which is sometimes hard to get your mind wrapped around if you come from a more traditional programming environment. A ‘Transform’ is a component of a game object. It cannot exist without a game object.

Your ‘transform.parent.gameObject’ is a correct way to get access the parent game object. You can access the script on the parent by:

var netview : NetworkView = transform.parent.gameObject.GetComponent(NetworkView);

or C#

NetworkView netview = transform.parent.gameObject.GetComponent<NetworkView>(); 

But since your link between different object in your prefab, you can also do drag and drop. Just declare your variable at the top of the file:

var netview : NetworkView;

or C#:

public NetworkView netview;

Then in the inspector you can drag and drop the parent object onto the ‘netview’ variable, and the Editor will create the link.

so the reason parent and child are transform related is because parent and child actually for gameobjects is a relation to eachothers position.

a normal gameobject position is its position relative to the parent

so for exmaple imagine a graph.

if i say its located at 2,4 what i mean is its 2 right and 4 above the center of the grid. its origin.

However what if i have this 2,4 object and i make it a parent

then I say i have a child whose position is 1,2

what that means is its 1 right and 2 above the parent.

which actually means its true world position is 3,6

however when I child something what i’m saying is its position from now on, make sure its position stays the same relative to the parent.

so if its position is 1,2 and the parent moves 1 right, then even though only the parent moved because i’m a child of the parent, I must move 1 right as well so that I can continue to be 1 right and 2 above the parent.

Childing a game object is just a way of saying whenever the parent moves I want the child to mirror that movement everytime.

Now when you look at is a mirroring a movement using the transform, the component that stores movement it makes sense. For example how can you make sure your 1 right and 2 above your parent except by checking to see what your parents position is, which is by going into your parents transform and asking it.

on the other hand

transform.parent.gameobject should in fact get you the parents gameobject.

You’ll have to show your code for that because that should behave as you expect.