I’m a bit confused about the hierarchy in Unity3D.
Ex.1: In a script attached to a game object (GO), the GO itself can be referenced with either gameObject or transform.gameObject if I understand correctly. What exactly is the point of this “double” reference to the same object?
Ex.2: From the docs about Object.DontDestroyOnLoad it says the usage is this:
function Awake () {
DontDestroyOnLoad (transform.gameObject);
}
Why not just use it like this: DontDestroyOnLoad (gameObject); ?
gameObject by itself means : the game object which has this current script. transform.gameObject refers to the game object of the transform component. transform.gameObject always refers to the same game object as gameObject, but if you are using OnTriggerEnter or similar functions, you may have to write hit.transform.gameObject (therefore, accessing the game object of the transform of the collider).
For the second question, sure, no need to bother writing transform.gameObject, go for gameObject (since they point to the same game object).
function OnTriggerEnter (other : Collider) {
Destroy(other.gameObject); // Destroy the game object which has the triggering collider. In other words, the other game object having entered the collider of this script.
Destroy (gameObject) // Destroy the game object which has this current script
}
You can see that, depending on what is on the left of gameObject, you destroy something…or something else.
I have some difficulties to explain things that I took for granted.
Simple example:
Imagine you are making a game.
You spawn NPCs dynamically and add their transforms to an Array to easily keep track of them and edit their positions.
You need to kill an npc, but you only have its transform variable - then you can use transform.gameObject to actually Destroy it or access any of its other components.
It isn’t necessary. Every component has a link to its gameObject. That doesn’t mean its always necessary to use them. It can be useful, though. If for whatever reason you only have a reference to a Transform, you can get the gameObject that way. The question ‘but why don’t you just use a GameObject directly’ isn’t that important, there could be many reasons why you’d only have a reference to a specific component and not its gameObject.
The idea behind the GameObject concept is to keep it as simple as possible. A scene can only contain GameObjects. A GameObject can have Components attached. They gave every Component a set of references to other attached components and to the containing GameObject.
Every Component is derived from the Component-class. The gameObject reference is the most important reference because it’s the link to the owner. The other references like: transform, rigidbody, animation, camera, … are really not necessary because you can get all those components by calling GetComponent() on the containing GameObject but they provide an easy access to some common components like transform.
If you execute code in a script (MonoBehaviour component) you are that component. So the “this” reference points to your script-component. Without the gameObject reference you have no clue to which GameObject your script belongs.
There is a huge amount of redundant references in Components and GameObjects but they just make your life easier.
You can do strange things like:
transform.gameObject.transform.gameObject.gameObject.transform
but it won’t help to read your code and slows down the execution.
The examples in the Unity Reference Manual are really great but they contain some tiny mistakes or things like:
DontDestroyOnLoad (transform.gameObject);
which should be:
DontDestroyOnLoad (gameObject);