In the previous post, we installed unity as well as Visual Studio. We added a GameObject within a scene in Unity. We got familiar with the user interface and we created a script. Now, we want to start scripting. Unity uses C# as a scripting language.
GameObject is a class that every game object in a scene inherits from. Every GameObject has a class called transform that includes information on where the object is in 3D space. There you can find variables for position, rotation, and scale of an object. You can add more components to this game object through the inspector, or through C#. For more information check the GameObject class in the Unity documentation.
Components inherit from the base class MonoBehaviour. Most of the scripting you will do in Unity will be in MonoBehaviours. Every public varliable you add to a class will be editable in the Unity inspector. This is a good way to access other components in other game objects by just dragging and dropping them in the editor. MonoBehaviour has some callback methods that are called by the engine under certain conditions. They are called Unity Messages. You can get more information on their execution order in the Unity documentation.
Unity does a lot of operations when constructing a MonoBehaviour class, that is obfuscated from a Unity developer. So, instead of using a constructor, Unity provides some Unity Messages that offer multiple Unity Messages with different execution order.
Awake() runs once after the object is constructed regardless if the component is enabled or not enabled in the Unity editor. It’s the first Unity message in the execution order.
OnEnable runs after Awake() and it will only run when the component is enabled in the editor. It can also run again every time the component is enabled.
Start() runs after OnEnable() and it will only run once if the component was enabled. It will not run again if the component is re-enabled.
This is very simular to a destructor, but in the context of Unity. If a GameObject is destroyed by the engine, this will be the function it will run with it dying breath.
Games are state machines. Most of the game logic is written in update functions. Some of them run more frequently than others. Here are some of them.
The Update() method gets called once every time the engine renders an image on the screen. For example, if the game is running at 60fps, then the update function will run 60 times a second. You will be writing most of your game logic in this method.
This is almost identical to the Update() function, but instead of the updating every frame, it will update more frequently according to the physics engine steps. Physics engines calculates steps more frequently than the frame rate usually.
This is an update method that runs after the Update functions, in case you needed to do calculations after the frame has been rendered. It also runs every time a frame is rendered on the screen.
There is a static function in Unity called Debug.Log() that you can pass arguments to it, and the value and type of that argument will be printed in the Unity editor console. This function will be your best friend when trying to debug you code real quick.
For more information, check the MonoBehaviour class in the Unity documentation.
Please check our full video covering this topic in our video beginner series for Unity.