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.
- GameObjects
- 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.
- GameObject 1 - With the script
- GameObject 2
- 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:
- Which Object am I accessing?
- You didn't access anything. Btw, there is only one gameObject.
- B itself?
- B is a component, not an object.
- Can I attach components to components?
- No you can't. This explains why you're not accessing an object. You can access variables.
- 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'.
- Is calling a.GetComponentInChildren() the same as b.GetComponentInChildren()?
- Not in your example, because 'b' isn't variable.