What are "gameObject" and "transform" variables from a script in Unity

Опубликовано: 08 Октябрь 2024
на канале: GDT Solutions ES
1,977
32

In this video we see what the variables "gameObject" and "transform" mean inside a Script in Unity. It is important to take into account the use of capital letters, we are not talking about the classes "GameObject" and "Transform", but the variables "gameObject" and "transform" that are defined in all MonoBehaviour in Unity.
Specifically these variables refer to the OBJECT TO WHICH THE SCRIPT IS ASSIGNED, in Unity the Scripts that inherit from MonoBehaviour (for example any script created by default in Unity) can be assigned to the GameObjects of the scene and this makes Unity automatically execute functions like Awake, Start, Update or FixedUpdate if the GameObject that has the script assigned to it is active in the scene.

The variable "gameObject" refers to the object type GameObject to which the Script (where we use the variable gameObject) is assigned, therefore the variable "gameObject" will point to a different object depending on which Script it is and moreover, to the particular instance of the Script that executes the code.
To understand this last one we must think that the same Script can be assigned to all the GameObjects that we want, each assigned Script is an independent instance of the others, therefore the variable "gameObject" inside the same Script will point to a different GameObject depending on the instance in question.

The variable "transform" on the other hand points to the Transform component of the GameObject to which the script is assigned, since every GameObject in the scene in Unity has at least one Transform component assigned to it. And as mentioned above, a Script can be assigned to multiple GameObjects in the scene, so the "transform" variable will point to a different Transform component depending on the instance in question.

The variables "gameObject" and "transform" in Unity allow us to know at any time the position, rotation and scale that has in the world the GameObject to which the script is assigned, through the Transform component we can execute functions such as Translate and Rotate to move and rotate the object to which the Script is assigned.

INTRODUCTION VIDEO OF THE REFERENCE SERIES IN UNITY:
   • Video  

ARTICLE ON THIS TOPIC:
https://gamedevtraum.com/en/game-and-...

____________________________________________________________
LINKS

Portfolio: https://gamedevtraum.com/en/portfolio...

LinkedIn:   / gamedevtraum  

Downloads: https://gamedevtraum.itch.io/

Contact 1: [email protected]

Contact 2:   / gamedevtraum  
____________________________________________________________