Sponsored By

Basics of Unity scripting

Unity is one of the most used game engines by new developers but they often struggle with the script part. This post will guide you through the basic path to follow while learning Unity scripting.

Vinod Ravishankar, Blogger

February 8, 2021

3 Min Read

Scripting is an integral part of game development. You can either code or do visual scripting depending on your choice. But, learning to script is required no matter what game you are making. The advantage of learning scripting for a game engine is that you don't need to learn all aspects of the programming language. For example, Unity uses C# as its coding language but you can code a game without knowing concepts like inheriting, encapsulation, etc. All you need to know is what are the basic functions needed to make a game. In this post, we will see the basic functions that you need to know in Unity.

If you add a script to any Unity object you will see that two functions are added by default in the script. One is the Start function and the other is the Update function. Apart from these, the OnCollisionEnter function is also an important one. Now let's see these functions in detail.

The Start Function

The Start function of a script is called only once in the lifetime of the gameobject. That is wherever the gameobject is made active the Start function in the script attached to the gameobject is executed. The function is not called after that till the object is deactivated and reactivated again. Unity Awake is also a function similar to Start. You can read more about Unity Awake vs Start in this post in Vionix Studio.

The Start function is mostly used to initialize variables in Unity. For example, you have a player character you can set the health of the player to 100% in the start function so that every time the player is spawned his health is set to 100.

The Update Function

The update function is the most useful function in Unity scripts. The update function is executed once every frame. If your game is running at 30 fps then the Update function is executed 30 times per second. This is very useful to add codes that need constant execution. For example, if you need to move a player whenever the forward arrow is pressed. In this case, you need to constantly scan for the keypress and also move the player when the key is presses. You can do this in the Update function.

The OnCollisionEnter Function

Collision detection is needed in almost all games. You need to know if your player has hit an enemy or a wall or just swinging his weapon in the air. Most game actions require collision detection to decide on the further proceedings. Like, if you make a sword fight game, you need to know if the player is damaged by the enemy sword or the enemy is damaged by the player sword. You can do this using the OnCollisionEnter function in Unity.

You need to add a collider component to the gameobject for the OnCollisionEnter function to work. 

Conclusion

 With these basic functions, you can easily start to code your game and also make and publish a small game. So start to code now and remember to start small. Have fun making games.

Read more about:

Blogs
Daily news, dev blogs, and stories from Game Developer straight to your inbox

You May Also Like