Whats the difference between the Component and GameObject classes and how they are related?

Im trying to understand the Unity class framework and I couldn't figure out why Component has the same variables as GameObject. Component and GameObject have a field called Transform, which is a component. For example, lets say that

GameObject a;
Transform b;

and b is attached to a, if I do

b.Transform

which Object am I accessing? b itself? Can I attach components to components?

If I create a variable of type GameObject called c on the GameObject a and I call GetComponentInChildren(), will its search area include c? EDIT: I miss understood the parenting concept. Check this page for info Transform (scroll down to "parenting").

Is calling a.GetComponentInChildren() the same as b.GetComponentInChildren()

Thanks

  • GameObjects are all the same type.
  • Components come in many types.
  • GameObjects contain (instances of) components.
  • Every GameObject contains a Transform component.
  • Every component can access its gameObject.
  • Every component also get convenience accessors (properties) that retrieve the corresponding properties of its gameObject, including .transform, as you have observed.

Reading the docs might help ;)

The relationship between GameObjects and Components is explained in the manual here:

The GameObject-Component Relationship

Both the GameObject class and the Component class have a number of properties that are "shortcuts" to (other) components.

`myGameObject.transform` is a shortcut for `myGameObject.GetComponent()`.

`myComponent.transform` is a shortcut for `myComponent.gameObject.GetComponent()`.

Incidentally, the GetComponent function exists on the Component class as well, but that is also a "shortcut" to let you write less verbose code.

There are also some functions that exist in multiple places for your convenience, such as GetComponentsInChildren(), like you mention.

GameObject a; Transform b;

Line one makes the variable 'a' point to a gameObject.

Line two makes the variable ‘b’ point to the transform of a gameObject.

With 'a' we can access the gameObject class's variables and functions, which can be found here.

With ‘b’ we can access the transform class’s variables and functions, which can be found here.

Note: The gameObject class can access the same variable as the transform class!

Say I want to move an object somewhere.

Using ‘a’ we get this :

function Update()
{
    a.transform.Translate(x,y,z);
}

That is the same as "gameObject.transform.Translate(x,y,z)", where 'a' replaces gameObject.

Using 'b' we get this :

function Update()
{
    b.Translate(x,y,z);
}

That is the same as "gameObject.transform.Translate(x,y,z)", where 'b' replaces gameObject.transform.

Because 'b' is actually a transform component, it has to be connected to a gameObject. So really you connected to a gameObject but decided to only focus on the transform and "ignore" the other aspects of the gameObject. This makes things simpler for you and the compiler.

Note: Both scripts above will pull off the exact same desired effect.


Now, in your example you suggested that 'b' is the transform component of 'a'. You'd then be allowed to use "a.b.Translate(x,y,z)" instead. But it is unnecessary to reference the same object twice when once works perfectly.

If you were to use “b.transform.Translate(x,y,z)” it wouldn’t work. Why? Because the Transform class doesn’t have a transform function or variable in it! If we replace the variable for what it really is, you can see the problem. “transform.transform.Translate(x,y,z)” won’t work at all! There is no object being called!

Both gameObjects and components use the transform variable. Components' transform is the same as the transform that the component is attached to. The same is for the function GetComponentInChildren(). But when you asked "Is calling a.GetComponentInChildren() the same as b.GetComponentInChildren()" you asked it thinking that 'b' was the transform component, when is actually a transform variable.

  1. GameObjects
    • Components
      • Variables
  2. Functions

    Using a function to change the variable will change it for both the gameObject and Component. Both can reach it and change it. Which functions you can use depend on the component.


"If I create a variable of type GameObject called c on the GameObject a and I call GetComponentInChildren(), will its search area include c?"

In this question there are three gameObjects : The gameObject who has this script attached to it, the gameObject being referenced by the 'a' variable, and another gameObject being referenced by the 'c' variable.

  1. GameObject 1 - With the script
    • Script
      • Variables 'a' and 'c'
  2. GameObject 2
  3. GameObject 3

GetComponentInChildren wouldn't find anything because 1. Using a gameObject as a variable, doesn't mean it becomes its child. 2. It only looks for components, not variables or gameObjects. You use those components to find the variables and gameObjects. 3. That function will be looking in gameObject 1's children, say gameObject 1.1, for something that's already "inside" gameObject 1.


Ugh, sorry for being like the department of redundancy department.

TL;DR

If you really want the short and sweet answers, here they are:

  1. Which Object am I accessing?
    • You didn't access anything. Btw, there is only one gameObject.
  2. B itself?
    • B is a component, not an object.
  3. Can I attach components to components?
    • No you can't. This explains why you're not accessing an object. You can access variables.
  4. If I create a variable of type GameObject called c on the GameObject a and I call GetComponentInChildren(), will its search area include c?
    • It will not look for 'c', or find 'c' the way you suggested. It will look for the component that uses 'c', and you can use that to find 'c'.
  5. Is calling a.GetComponentInChildren() the same as b.GetComponentInChildren()?
    • Not in your example, because 'b' isn't variable.