Keeping score

We now want to make it so that when we collect all of the orbs in the level the goal will appear and then you will win the game when you touch it:

  1. Go back into MonoDevelop and select the GameController class. Then, add the following variables:
    public static GameController _instance;
    private int orbsCollected;
    private int orbsTotal;

    Note

    Although it may make more sense English-wise to use totalOrbs and collectedOrbs programming-wise, putting the common word first means that when you start typing orbs, it will show both options for you when working with code completion in your own projects.

  2. As normal, we will need to set these variables as well in the Start function:
            GameObject[] orbs;
            orbs = GameObject.FindGameObjectsWithTag("Orb");
            
            orbsCollected = 0;
            orbsTotal = orbs.Length;

    Make sure that you place this after the BuildLevel function or there will be no orbs for you to count!

  3. We will also want to initialize the _instance variable, but instead of using Start, we will be using Awake:
        void Awake()
        {
            _instance = this;
        }

Awake gets called before Start, which is important because we have to initialize the _instance variable before you use it, this is known as a Lazy Singleton.

Singletons

As you work in Unity, you may find that you have certain managers such as our Game Controller that we will only have one of. Rather than having to have other objects store them as variables or find them at runtime, we can use a design pattern called the singleton pattern. The gist of it is that there is one and only one object of this class that can be created. The version that I am using is the quickest way to get singleton-like behavior going.

Note

Never use the GameController._instance variable inside of another Awake function as you are not guaranteed the order in which they'll be called. However, if you use it in Start or any of the other functions we've talked about, you'll be okay.

As we collect orbs, we want to increase the value of our orbsCollected variable. Rather than just giving other things access to the variable, let's wrap this around a function so we can do other things, such as update the GUI later creating a function:

public void CollectedOrb()
{
    orbsCollected++;
}

In our OrbBehaviour script, we call the function:

    void OnTriggerEnter(Collider other) 
    {
        GameController._instance.CollectedOrb();
        Destroy(this.gameObject);
    }

When you access the _instance variable, you get access to the public functions and variables that exist in the class.

Now that we have this data stored, let's display it on the screen so players can see. Go to GameObject | UI | Text. Place it on the top left of the screen (a position of 0, 1, 0 with an Anchor of upper left and alignment of left) using the Anchor Presets menu we used in previous chapters. Next, give the object a name of Score Text and give it the Pos X of 10 and Pos Y of -10:

Singletons

Now that we have the text object created, let's reference it inside code. To start off, we will need to add in the correct using line at the top of the Game Controller script:

using UnityEngine.UI; // Text

Then, we will need to add one last variable to our Game Controller:

public Text scoreText;

We need to initialize it in the Start function:

    void Start() 
    {
        BuildLevel();

        GameObject[] orbs;
        orbs = GameObject.FindGameObjectsWithTag("Orb");

        orbsCollected = 0;
        orbsTotal = orbs.Length;

        scoreText.text = "Orbs: " + orbsCollected + "/" + orbsTotal;
    }

And now because we have a text displaying the orbs, we can now update our text accordingly:

    public void CollectedOrb()
    {
        orbsCollected++;
        scoreText.text = "Orbs: " + orbsCollected + "/" + orbsTotal;
    }

With this, save the script and then go back into the Unity editor. Once you reach here, set our newly created variable with the Text object we created and then click on the Play button:

Singletons

At this point, you can move around the level, and when you collect the orbs, they will now update the GUI letting us know how many coins you've collected and how many there are total in the level. We're making great progress and we almost have a full game, we just need one last thing, a way to win!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset