Unity is a very powerful game engine. Full power can be utilized only when you can master unity scripting. The basic unity scripting is very easy to learn, and can be picked up by anyone. Though Unity visual scripting tools can help you make games without scripting, learning to script can give you better control over the game engine.
Unity scripting is based on C#. A basic knowledge of variables, functions, classes should be enough to get you started in unity. Even if you haven’t worked with C# and have the knowledge of any other coding language, then you should get the hang of it in a few minutes.
The best part of Unity scripting is when you make a new script the basic layout of classes and function is made by default. As you can see in the Screenshot below, when you make a new script two functions are made available for you. They are the “Start” function and the “Update” function. Note the first letter of these functions are in capital letters.
The Start Function
Start function in Unity is called when the Gameobject to which the script is attached is created. If you are totally new to Unity, then read our article on how to learn unity first. Start function is called only once during the object lifetime, i.e. if the object is created and destroyed, then the Start function is called again.
So why do you use the Start function? Start function is used to set initial values to some variables and to spawn some objects needed with the Gameobject. For example, when you spawn a character, you need to set the health of the player to 100. You might need an enemy to spawn when the player is spawned. You can use the Start function for these.
The Update Function
Update function in unity runs every frame of the game. The Update function is called on every frame when the Gameobject to which the script is attached is active. Update function has another variant called the FixedUpdate. Fixed update runs for every physics cycle. The difference is FixedUpdate is called for the same time interval in all devices. But, the time interval in which update is called depends on the device in which the game is running. As every device runs the game at different FPS.
Update function is used for things that require continuous commands. For example, moving your game character. When moving your character, you need to check your character position every frame and adjust the direction and speed depending on the target position. Say your target is a moving one then you need to check your target position every frame too.
Other most commonly used classes and functions
GetComponent is used to refer to the component of the Gameobjects. Like Rigidbody, collider, animator etc.
Application.quit is used to quit the application when running in standalone build. An important thing to note is it doesn’t work in editor mode.
Audioclip is used to contain audio data. If you want to have music in your game then this class will be useful.
Transform is used to know the position, rotation and scale of a Gameobject. It is better to learn a little about vector2, vector3 and Quaternions.
Camera view is the portal through which a player sees the game. It gives you many parameters that you can control to change your game view from script. Also, camera world to screen point and screen to world point conversion is something you should master.
Mathf is useful if you want to perform mathematical calculations like absolute value, radian to degree conversion, sin, cos, tan etc. in your script.
This is used to detect a collision between non static Gameobject. OnCollisionEnter function is called when a collision is detected.
OnTriggerEnter is similar to OnCollisionEnter, but the difference is, it is called when two non-static objects with trigger colliders enter each other’s space.
Loadscene function of scenemanager is used to load different scene when called upon. You can pass the name of the new scene or the index of the scene to load it.
Scenemanager can be used for many other functions like moving a Gameobject from one scene to another, get the name of the current scene etc.
10. Input System
Input, as the name suggests, is used to get input from users. Some widely used functions of input are input.getaxis, input.touch, input.getbutton. This is used to move a player using arrow keys in keyboard or based on touch input.
With the knowledge of the above class and functions you should be able to make the most of the games in Unity. Reference Unity Docs.
11. Unity instantiate prefab
Instantiating a prefab in Unity is very commonly used in games. You need to know how to efficiently instantiate a prefab in Unity.