Unity – 2D Spaceship & Function lesson

Today we’ve had a lesson on learning Functions (aka methods and subroutines) in Unity. The goal of today was to understand the concept of and define a function and understand Unity’s message system.

I’ve found out that a function is a name of a reusable collection of instructions which is written to perform a task.

void Start () {

void Update () {

Update is the name of a function and its used once per frame by Unity’s engine.

A function has three main parts, 1) a return type, 2)a name (or identifier) & 3) a set of parameters. So;

1 2 3

void Start () {

void OnCollision (Collision collision)

And a function call has two parts; 1) the functions’ name & 2) a set of parameters if they are needed.

1 2

Destroy (gameObject)

Functions are used for tidying up the code you write so you don’t have to write the same code text over and over.

Continuing our Spaceship game we’ve explored adding buttons inside the game and into our code. The button began with a simple function just to say ‘Hello’ at the bottom of the screen when pressed but more code was added to allow the spaceship game to start and to proceed to the next level if desired.

So we followed the instructions and the button appeared….

Quite why it’s that big I don’t know, all I do know is that it’s that big it could be an end of game boss in its own right. And I don’t want my ship to be anywhere near it when it inevitably explodes. Right after we had the button click to say hello, we altered the code to have the button proceed to the next level of the game.

We also altered the original code for the ship’s movements to shorten it by adding functions so we wouldn’t have to waste time writing and repeating the same chunk of code over and over again.

From here on, the rest of the lesson was our own to work on the spaceship project. I did experiment a little in trying to create some assets to use but I had PC issues in class and as a result I lost some work. When I did get up and running properly I chose to watch some of Ant’s videos on Moodle about coding in unity to see if I could get anything to sink in. I don’t think it worked.

As always I feel like I don’t get coding at all and I don’t think I’ve had a lot of experience with Unity’s coding side outside of lessons as I’ve been busy with other college projects, a certain Maya one in particular. Now I’m free of it, I intend to dive right into Unity and see if I can figure something about it in the hope I start to know what I’m looking at and what it does so I could at least attempt to produce something myself, or know where a lesson is going and what its purpose is instead of not knowing what I’m doing all of the time. What I’d like in a perfect world is to be able to sit down with someone and have a bit one-to-one time to see if I can learn something about it but with so many other students I can’t see it happening. I also need to crack on and see if I can make some good spacey type assets I can use in my ifIevergettheregame.

So its down to me and countless tutorials to see if I can get something to sink in.

Search

Search for:

Text Widget

This is a text widget, which allows you to add text or HTML to your sidebar. You can use them to display text, links, images, HTML, or a combination of these. Edit them in the Widget section of the Customizer.