Unity 102: Scripting

Abdullah Hamed - Nov 19 '20 - - Dev Community

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.

Game Objects

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

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 Messages

Initialization

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()
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()
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()
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.

OnDestroy()
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.

Game Loop

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.

Update()
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.

FixedUpdate()
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.

LateUpdate()
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.

Print to console

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.

More resources

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.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .