GameObject vs Transform : Unity

Siratim Mustaquim - Jul 15 - - Dev Community

In unity there are these questions related to Transform and GameObject, such as,

  1. Which one to return as parameter?
  2. Which one to receive as a serialized field from Editor?

Here goes the simple reference points to make the decision

What you get with Transform :

  • Transformational info of a GameObject: position, location, scale (local and world)

  • Parent Child Transform Information : It handles the transformational information between parent and child GameObjects

  • Gives reference access to the attached GameObject

What you get with GameObject:

  • Permission to attach components: add multiple components / scripts (AddComponent())

  • Lifecycle management : Create, destroy, enable and disable the GameObject along with the components.

What to Choose ?

First match your use case with the above. Pick from that. Then consider the below issue

GameObject can be enabled and disabled, Transforms can't be disabled. Having a reference of a GameObject can return null if it is disabled. At that stage enabling that gameobject is not possible via script as the reference is null ! In scenarios like these passing a Transform or receiving a SerializedField as Transform via Editor makes more sense here. And you can always get the reference of the GameObject by using the gameobject property of the Transform !

Happy Coding !

.
Terabox Video Player