Back to Top

Home ยป Tutorials

Unity Game Programming Tutorials

Figuring out the architecture of a typical script

Last time we created a simple script to reset the position of our player. If you are an advanced programmer, I guess that you may be quite bored now. But I'll assume that (since you have visited my site) you are a beginner who wants to learn game programming from scratch, so I'll go through each line of C# code inside our PlayerReset script, explaining what it does.

Each modern C# application needs to use one or more prebuilt libraries; otherwise, we would waste our precious time rewriting the code for many of the existing classes and methods.

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

The three lines of code above add all the interfaces and classes which are included in System.Collections, System.Collections.Generic and UnityEngine to our arsenal. The System.Collections namespace defines lists, queues, bit arrays, hash tables, dictionaries and more, while System.Collections.Generic includes a set of strongly typed collections which have improved safety and performance. Finally, "using UnityEngine" gives us access to Unity's namespace, which includes all its classes and interfaces. You don't need to memorize what these lines of code do, of course; just keep in mind that Unity will add them to the source whenever you create a new script.

public class PlayerReset : MonoBehaviour

{

    // Start is called before the first frame update

    void Start()

    {

        transform.position = new Vector3(0, 0, 0);

    }


    // Update is called once per frame

    void Update()

    {

        

    }

}

The rest of the code is included in a public class. In object-oriented programming, a class is a template which includes member variables and methods, allowing us to create various objects. Public classes are available to, and can be accessed by any other class.

The name of our class is PlayerReset (identical with the name of the script file) and it inherits the properties of MonoBehaviour, Unity's base class. I know that this may sound a bit too technical, but let's simplify things a bit and say that because of that line of code, PlayerReset has gotten access to Unity's built-in functions.

The class includes two methods: Start() and Update(); we have added our custom line of code to the first one, remember?

void Start()

{

        transform.position = new Vector3(0, 0, 0);

}

Start() is only run once, when we start the engine, and then stops. This makes the method perfect for initializing object properties, computing values that won't change during the game, and so on.

Let's take a look at the line of code which has been added to the script:

transform.position = new Vector3(0, 0, 0);

Since our PlayerReset script is attached to the Player object, it can reference all its properties without specifying its name. So, our line gets access to Player's transform.position and sets its X, Y and Z components to 0, 0, 0. Here's an image which was created using the Inspector view and should explain what's happening clearly.


player transform

As you can see, we access Player's transform, then its position, and then we set its position (X, Y and Z values) to 0, 0, 0, placing it in the origin of the coordinate system.

Now that we've gotten this out of the way, it's time to move on to the second method.

void Update()

{

        

}

The Update() method is run much more often; in fact, it runs once per engine frame. So, if your game runs at 60 frames per second (fps), Update() will be run 60 times each second as well. As you can probably guess, this method is used for game elements which need to change their values and properties at runtime.

Want to find out if the player has pressed the space key, and then fire a bullet? Then, you will need to put that code inside Update(). On the other hand, if you plan to create a function which computes the value of pi, you can place its code inside Start(), because you'll only want to do this once, right?

That's the end of our tutorial. I hope that you've learned a lot, and (hopefully) you look forward to reading the next one.