ROLL-A-BALL TUTORIAL #6

#Adding UI of this game
We add UI of this game. It is a very simple function. it is only “Display the number of remaining items” It has the following function.
1.	Count the number of remaining items.
2.	Display items by UI

 ![図1.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/5fcca524-669a-669b-70c3-b4dcaa5342d9.png)


#1. Preparing UI of displaying “the number of remaining items”.
At first, Let’s create the UI. Simply, We create a UI that displays the number of remaining items in the bottom left on the screen.
##1-1. Set the “Canvas”
At first, We create “Canvas” that displays UI.
1.	Click the “Create” of the “Hierarchy” view.
2.	Click “UI → Canvas”.

![図2.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/87575889-0fe4-8d3a-2f76-943b053383db.png)
 
Set the supposed size for the “Canvas”.
1.	Click the “Canvas” object in “Hierarchy” view.
2.	Set the "UI Scale Mode" to "Scale With Screen Size" in Canvas Scaler.
![図3.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/3adb4560-43fc-47f5-010c-9e849c13219c.png)
 

##1-2. Set the “Text”
Next, we create the text.

1.	Click the “Canvas” object in “Hierarchy” view.
2.	Click “UI → Text”.
3.	Rename the created Canvas> Text object as ScoreLabel in the Inspector view.

![図4.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/779e0967-7ef4-8315-f136-faebe610600a.png)
 
You can be seeing “Text”.
Move the “Text” to the center and adjust the font size.

1.	Click the “Canvas>ScoreLabel” object in “Hierarchy” view.
2.	Set the value of “Rect Transform” to the following. (PosX : -230, PosY : -230, Width : 300, Height : 100)
3.	Set “Font Size” of “Text” to “60”.
4.	Set “Text” of “Text” to “0”.
 
 ![図5.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/85133b5d-713c-53c6-c26a-d12e0ce1aa23.png)


![図6.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/149e10fd-a129-5ba0-e24b-d79af75bf0d3.png)

 
#2. Create the “Game Controller”

Next, we create a system to notify the “ScoreLabel” that created “the number of remaining Items”.

There are many ways to implement this system, but this time we create it by simplist way that “GameController” manages. And in Unity "GameController" should manage to the progress of the entire game such as the number of items. This "GameController" counts “the number of remaining Items” and has a system to notify if it is changing.

Let’s create this "GameController".

1.	Click the “Create” of the “Hierarchy” view.
2.	Click the “Create Empty”.
3.	Rename the created object name to “GameController”.
4.	Change the “Tag” to “GameController”.

 ![図7.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/41d26909-74e6-8e63-12ec-7ec4979fdaf7.png)



 
#3. Preparation for counting the items.
##3-1. Set the “Tag”.
We set the "Item" tag to "Item" object and count the number of object that has this tag.

1.	Click the “menu bar> Edit > Project Setting > Tags And Layers” 
2.	Open the “Tags” menu in the “Inspector”.
3.	Open the tags by clicking the “+” button.
4.	Rename “New Tag” to “Item”.

![図8.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/6567387e-0f33-a32c-7ccd-e222ba8a33b1.png)

 
##3-2. Set "Item" tag to "Item" object
We set the "Item" tag. “Item” tag was added to the list because registered the Item tag already.

1.	Click the "Item" object of the “Hierarchy” view.
2.	Change the “Tag” to “Item” on the “Inspector” view.
3.	Click the “Apply” button and update “Prefab”.
 
![図9.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/fbac9559-b571-4473-6a33-ecab4855d446.png)


##3-3. Add a function to count “Item” in “GameController”.
At first, we create “GameController”.

1.	Click the “GameController” on the “Hierarchy” view.
2.	Click the “Add Component” on the “Inspector” view.
3.	Click the “NewScript” and create “GameController”.

Write the following code in the “GameController”.
 
```Item.cs
using UnityEngine;
 using System.Collections;

 public class GameController : MonoBehaviour
 {
     public void Update ()
     {
         int count = GameObject.FindGameObjectsWithTag ("Item").Length;
     }
```

##3-4. Display the number of remaining items.
We modify to display a number of remaining items to created UI.
At first, we think the way to connect “Text” and “GameController” Uses a way to set a reference to the ”Text” by the source code.
Create a variable for reference.
 
```Text.cs
using UnityEngine;
 using System.Collections;

 public class GameController : MonoBehaviour
 {
     public void Update ()
     {
         int count = GameObject.FindGameObjectsWithTag ("Item").Length;
     }
```

Next Assign number of got Item to "Text".
 
```Text.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 ();
     }
 }

```

#4. Connect "GameController" and "ScoreLabel".

Finally, we connect "GameController" and "ScoreLabel".
1.	Click the “GameController” on the “Hierarchy” view.
2.	Drag and drop the “Canvas> ScoreLabel” object to the “ScoreLabel”.

 ![図10.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/1fb632c2-a95a-4842-c2ac-9d4fad46a724.png)


Let’s start the game again. When playing the game, “the number of remaining items” is displayed in the bottom left and you can see that the remaining number decreases each time you get the item.

![図11.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/581ebd05-62a0-0896-0b7e-6be18f54226b.png)