ROLL-A-BALL TUTORIAL #7


Add end of the game

We add conditions for the end of the game.
So we should add the function.
It is the function that displays “YOU WIN” on the screen when the game player collects all items in the game.

The necessary elements to realize the function is as follows.
1. A word to mean the end of the game.
2. A judgment that finish collecting all item.

1. Make a UI of end of the game

1-1. Make label.

At first, we make a UI at end of the game.

  1. Click the “Create” button in the “Hierarchy” view.
  2. Select “UI” -> “Text”.
  3. Change the name of “Text” into “WinnerLabel” in the Inspector view.


1-2. Set the value of the label.

Next, we set the value of the “WinnerLabel” object.
1. Select “WinnerLabel” object in “Hierarchy” view.
2. Set the value of “RectTransform” in the “Inspector” view. It is as follows.
(PosX:0, PosY:0, Width:300, Height:100)
3. Change “Text” into “YOU WIN” in the Text component.
4. Set “FontSize” into “60” in the Text component.

2. Set conditions for the end of the game

We set conditions in Game Controller.

The game player finishes collecting all items. It is the conditions of victory of the game.
Therefore, the game player wins if the number of items becomes Zero.

  1. Double click “GameController.cs” in Project browser to boot “Monodevelop”.
  2. Change source code. It is as follows.
GameController.cs
using UnityEngine;
using System.Collections;

public class GameController : MonoBehaviour
{
    public  UnityEngine.UI.Text scoreLabel;

    public void Update ()
    {
        int count = GameObject.FindGameObjectsWithTag ("Item").Length;
        scoreLabel.text = count.ToString ();

        if (count == 0) {
            // program when game player wins.
        }
    }
}

3. Display Label when the game finished

We display “YOU WIN” on the screen when the player completes the game.

3-1. Set referring from “GameController” to “WinnerLabel”.

  1. Open “GameController.cs” by “Monodevelop”.
  2. Change source code. It is as follows.
GameController.cs
using UnityEngine;
using System.Collections;

public class GameController : MonoBehaviour
{
    public  UnityEngine.UI.Text scoreLabel;
    public  GameObject winnerLabelObject;

    public void Update ()
    {
        int count = GameObject.FindGameObjectsWithTag ("Item").Length;
        scoreLabel.text = count.ToString ();

        if (count == 0) {
            // program when game player wins.
        }
    }
}

After change source code, return to Unity Editor.
We set referring from “GameController” to “WinnerLabel”.

  1. Select “GameController” in the “Hierarchy” view.
  2. Drag-and-drop “WinnerLabel” of “Hierarchy” view onto “winner Label Object” of the GameController component of the “Inspector” view.

3-2. Hide object of “YOU WIN”.

We can hide the object in several ways.
This time, we choose the way to deactivate Object.

  1. Select “WinnerLabel” in the “Hierarchy” view.
  2. Uncheck the object in the “Inspector” view.

3-3. Display Label when the game finished.

We change the status of “WinnerLabel” from inactivity to activity.
Change source code. It is as follows.

GameController.cs
using UnityEngine;
using System.Collections;

public class GameController : MonoBehaviour
{
    public  UnityEngine.UI.Text scoreLabel;
    public  GameObject winnerLabelObject;

    public void Update ()
    {
        int count = GameObject.FindGameObjectsWithTag ("Item").Length;
        scoreLabel.text = count.ToString ();

        if (count == 0) {
            // Change status of object to active.
            winnerLabelObject.SetActive (true);
        }
    }
}

4. Play the game

Let’s try to play the game.
Game player collect an item, and finally "YOU WIN" appears on the screen.