Asteroids – Functions/Methods
Functions are groups of code contained in a class which do a certain job. In other words they help keep code organised.
Functions are also known as methods. I usually refer to them as methods in my tutorials.
If we take a look at our MyScript script, we already have some functions. These functions are the Start function and the Update function.
These functions are special functions built into Unity. They will only work if the class is derived from monobehavior ( “: MonoBehaviour” next to the class name shows it is derived from MonoBehaviour) and the script is attached to an object which is enabled in the scene.
The Start method only runs once at the start of the scene (hence it’s name). The Update function runs “once per frame”.
What does this mean? A frame is a cycle where the game runs all its calculations and updates the scene. The term FPS (frames per second) shows how fast a game is running. The sweet spot for games is usually described as 60 FPS. This is the point where there is no visible lag in the game.
There are other built in functions to Unity which are very useful:
|Function||What it Does|
|Awake()||Runs only once before the Start method|
|OnEnable()||Runs when the object is enabled (before the Awake function if at start of scene)|
|OnDisable()||Runs when object is disabled|
|OnGUI()||Used for rendering and handling GUI events|
|FixedUpdate()||Runs at a fixed frame rate (Update frame rates can vary)|
|LateUpdate()||Runs after Update|
It should be noted there are a lot more but the ones in the table are probably the most used ones.
So how do we define a function in our code?
You may have noticed the Start and Update functions are void functions. This means that they do not need to return anything.
Like variables functions have types and with the exception of void functions they need to return something of the functions type.
That probably makes no sense to you right now but lets quickly do an example.
Add a new function to MyScript called TestMethod. Make it’s type string.
Notice how there is a red line under functions name. If we move our mouse over the name it says something about not returning a value.
So how do we fix this? Declare a new string inside our TestMethod called testString. Set this equal to our player’s name. Then on the next line type “return testString“.
Notice how the red line has gone.
Methods can be passed values. Have you noticed the () after all of our methods? Between these brackets is where we can pass values.
Pass TestMethod a string variable called message. See the picture below on how to do that.
Now where we set testString add a plus sign (+) after playerName. Then type ” ” (there is a space between the quotation marks) and add another + after it. Finally type message.
Note: What we just did there is called concatenation. This basically means we stuck some things together. If we didn’t have the ” ” in between the two variables we would get something like “PlayerNamemessage” instead of “PlayerName message”
So what now? Well at the moment our method does not actually do anything because it is not called anywhere. We will call it in our Start method. To call a function you simply write the name of the function and pass it anything that is required. Pass it the string “is making a game!”. This is our message variable.
This still won’t do much as we are not doing anything with the function. Declare a new string called functionMessage in the Start method and make it equal to the function call.
On the next line write Debug.Log(functionMessage). This will display our message in the console when we start the game. Debugs are very useful, especially for prototyping and finding out where your code is broken if you have an issue.
Test this now by entering your name into the player name box on the Player object and hitting play. In the bottom left corner of unity you should see “[Your Name] is making a game!]. You can expand the console window by clicking on this text.
One last thing should be mentioned about methods. They can be public and private just like variables. Make methods private if they are only being used by the class they are contained within and make them public if the method needs to be called from another script.
That concludes this part. In the next part we will cover if/else statements.