Adding a C# Script to Unity Game Project
Of course, having objects that don't move in our game doesn't really make our game interesting. So, let's understand how Unity C# Script can be used to move the game objects, and from there on we'll move to Rigidbodies.
First, of all, let's create a script. To do so, right click in the Assets area, and go to Create → C# Script.
This will create a new file, with the default name NewBehaviourScript. Rename the script to Movement, and press Enter. This will create a new script with name Movement in the Assets section. Double click on it to open it, and let's see what happens. Don't type anything in this script for now.
Since we're now moving into scripting, let me take a few moments to mention that Unity refers to objects in a scene as gameObjects. So, from this point forward, we will refer to the used objects in the scene as gameObjects. Mr. Star will now be referred to as a gameObject instead of an object.
You will notice that the script that we just created, comes with two pre-defined methods, and that your script automatically inherits from a base class called MonoBehaviour
. Let's go through these one by one and try to understand what they mean.
MonoBehaviour
This is the base class that all scripts inherit basic properties from, in Unity. This class defines and provides a lot of useful values, methods and properties which you can use in your script, saving you a lot of hassle. For example, MonoBehaviour contains definitions for the position of a gameObject (gameObject.transform.position.x/y/z
), meaning you can use these values directly instead of having to define them.
You generally shouldn't get rid of the inheritance declaration, since most of the time you'll need the stuff this parent class provides, to get your work done. MonoBehaviour also contains the definition for the Start()
and Update()
methods, which we will explain up ahead.
Start()
method
This method is run by the script once, at the very beginning of when the gameObject is initialized and is enabled. This means that this method is run as soon as an object becomes active. If an object is already active when the scene is opened, the initialization and enabling process are considered simultaneous. This method is very useful when you need to declare components or set values. For example, you can use the Start method to set the initial value of a gun's bullet count. You can also use it to access other components attached to any gameObject, as we'll see later on.
Check out the below code example, and do go through the comments to understand it.
Update()
method
This method is called 60 times per second by Unity (Or, 60 frames per second). It's where the main action of your code usually happens. For example, detecting input, adding forces, adding score, spawning enemies or bullets etc.
There are plenty of other pre-defined methods that MonoBehaviour class gives us. You can actually find a list of these methods in the Unity Documentation. They're for a variety of purposes, but for the time being, and for simplicity's sake, we will stick to just these two methods and the ones that we define on our own.