After installing Unity and going around the Unity editor, the next step in learning Unity is scripting. The basic unity scripting is very easy to learn, and can be picked up by anyone. But if you are totally new to C#, then read our post on basics of C# for Unity before continuing with this tutorial.
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.
Start function in Unity is called when the game object to which the script is attached is created. Start function is called only once during the object’s lifetime.
So, why do we use the Start function? Start function is used to set initial values to some variables and to spawn some objects needed with the game object. 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 activities.
Unity also has an Awake function, which is very similar to Start. You can read more on this in our blog post on Unity Awake vs Start.
Update function in unity runs every frame of the game. The Update function is called on every frame when the game object to which the script is attached is active. Update function has another variant called the
FixedUpdate 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. For more details on different types of Update function, read our post on Unity Update function.
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’s 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
Unity has many custom classes, that are used with Update and Start. We will see them one by one in the upcoming tutorials but here are few examples to give you an idea.
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.
Instantiating a prefab in Unity is very commonly used in games. You need to know how to efficiently instantiate a prefab in Unity.
In the next tutorial, we will start with the most important component, Unity transform.