2D Unity – Functions and Unity Menus

Much like in our last programming class, we broke down more terminology for things we already used in our walking sims but may not have fully understood at the time. Last week this was variables, this week its functions.

To quote the lecture slide word for word, a function is a named, reusable collection of instructions. For example we have used OnMouseDown and later OnRayDown, both of these are examples of functions, a collection of instructions that does a particular job. Also the bits of code on every new Unity script Update () and void Start () are also functions. I’ll check this in future but if we’ve been writing functions inside of other functions I wonder if that has a term, maybe a nested function? Programming seems to have a term for everything.

A function can be broken down as such:

  1. a return type (this defines the type of data that will be returned from the function)
  2. a name/identifier (pretty self explanatory)
  3. a set of parameters (not always needed but if they are they’re wrapped

The example given was:

int AddNumbers (int a, int B) {}

  • int = the return type
  • AddNumbers = the name/identifier
  • (int a, int b) = the set of parameters

Once created a function is easy enough to call back using only its name and parameters if needed name().

Once this had all been firmly embedded in our minds, we were given an example of how these kind of function calls can be used to clean up your code so its easier to read, otherwise known as refactoring (told you programming has a term for everything). This doesn’t really change your code, its more about restructuring it into smaller sections. To demonstrate this we were shown how to restructure our shipController from last week.


The script we were to simplify was our rotation and acceleration functions. Rather than cramming everything we needed into Update(), we created a function call for each, took the code out of Update() and instead had the function calls in Update().

It took me a while pondering over this in class to figure out why this was better than the other way, they both achieve the same job, you’ve just made the code longer and seperated it. I then had to consider how small this project it, if this was a huge game with every function crammed into Update() it would be a never ending wall of text. This way it keeps it as a list of functions which can be found elsewhere in the code.

The second part of this lesson expanded on the mechanic we played with last week, moving to a new scene when we collided with an object. However this time we would use the sceneManager to jump between the game and a menu. After creating a new scene we added a button to the UI using Game Object > UI > Button. Then aligned this to centre of the canvas using snap tools.


Currently the button has no functionlity, so naturally back to MonoDevelop.

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class ButtonController : MonoBehaviour

public void Click (string levelName)
 Debug.Log ("Loading level: " + levelName);
 SceneManager.LoadScene (levelName);

As you can see from above calling the SceneManager is pretty much the same as last week except we’re now using it on a Click action and also adding in a message to the console.


The scene to move to is set from the inspector, all buttons come with the On Click {} box by default. Once the action is set the string defined in the script will show up in the box and you can enter any scene in your project. So the first button was used to start my game (go to asteroids) and as you can see above a credits screen was also made which goes to a new scene with text on the canvas and a back button. It was quick and easy to setup this menu management once the reusable script was in place. Here is an example of it all in operation.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s