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.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/7fdf9b31-e5b9-8858-5b18-92d85788ed25.png)
 

##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.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/b46e1439-c918-d47e-697f-6171f425e53f.png)

![図3.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/47482b00-9d00-3b96-2873-f3644101bd31.png)


#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.
 
![図4.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/8c51d52b-006e-1d4c-3f1b-f265b367681d.png)


##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.
 
![図5.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/37b0a3d0-002e-5ac2-c863-d160034d1ecb.png)

##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.