Scripting: UI Variables

Step 1
In this lesson we'll learn how to add variables to the UI!
Step 2
Load/Open your project, you should be able to shoot objects/particles from the previous lesson.
Step 3
Create a UI/Text in your Canvas.
Temp1546564600 Temp1546564573 Temp1546564748

Rename it as "AmmoCount"

Step 4
Select the AmmoCount Text and type "Ammo: " in the text box.
Check Your Work
Step 5
Select your text, hover mouse over the Scene window and press F to focus on it, and then press 2D to view this.
Step 6
Open your Shoot.cs Script and we'll try to connect the UI to this script!
Step 7
Add these lines of code to your Shoot.cs script.
Step 8
What this code does.
Check Your Work
Step 9
Set the ammo to 5 and then hit play, try to shoot until you have no more ammo and you should not be able to shoot.
Step 10
Try creating a new collectible to add ammo!

What I did:
1. In the Shoot.cs script, create a function that will add 50 ammo;
2. Then I created a new script on the collectible object called "GivePlayerAmmo"
3. In the GivePlayerAmmo.cs script, make a onCollisionEnter function.
4. Also in the GivePlayerAmmo.cs script, make a public GameObject variable where the "robot" will go.
5. In the onCollisionEnter function...If the tag of the other object is a player...
5.1. Destroy this object.
5.2. Use this to add add ammo: robot.GetComponent().AddAmmo();

Temp1546817318 Temp1546817237
Step 11
Next we'll create text to tell how many keys are remaining on the current level!
Temp1547064615 Temp1547064634
Step 12
Locate your Canvas in your Hierarchy. And add another UI text object to it, name this as "KeysRemaining"
Step 13
Type "Keys Remaining: " into the text box of this UI Text object.
Step 14
Now add a new script named as "keysRemaining", attach it to this UI Text object.
Temp1547066059 Temp1547066096
Step 15
Open keysRemaining.cs and then type this code into your script.
Step 16
(You may need to click on this to enlarge and read it) Let's see what this new code does.
Step 17
Go back to your Canvas object in the Hierarchy, select it.
Step 18
Give this a tag called "KeysText"
Step 19
Open your EnableNext.cs script and add this new code into it.
Step 20
(You may want to click on this image to see it better) We'll see how to subtract a key when we touch a key.
Step 21
Go back to Unity. If your keys were hidden before, enable them all so we can see them all.
Step 22
You will want to disable these keys again when you press play, so make sure you know which keys will be hidden.
Temp1547067436 Temp1547067451
Step 23
Select your KeysRemaining UI Text in your canvas. Open the Disable Keys dropdown panel.

Type the number of keys you want to be disabled and then press ENTER.
I have 3 keys to disable so I typed 3 and I pressed ENTER.

Step 24
Click and drag the keys you want to be disabled into these elements.
Temp1547067747 Temp1547067777
Check Your Work
Step 25
Press play and make sure that your keys become disabled/invisible. And your Remaining keys count is correct.
Check Your Work
Step 26
Let's also test out if collecting them will reduce the Keys Remaining by 1 every time we touch one.

NOTE: If you get weird numbers like if it goes from 4, to 2, to -3, to -8, etc. This is because the keys are put too close to each other and using the GameObject.Find() function is slow so it takes time to update everything.

To fix this, wait a little bit between collecting each key, or place the keys further apart so the player will spend more time finding keys.

Step 27
Next we'll work on increasing score with collecting coins. So Create a UI Text in your Canvas called "Score"
Step 28
Just like with our other text, in the text box, type "Score: "
Step 29
Position your score text on your Canvas, I put mines on the top right of the screen.
Step 30
Important!! Make sure all text boxes have empty space on the right.

This is because in these empty spaces, a number will be shown. If there's not enough space, then we can't see the number.

Step 31
Also give this score text a tag called "ScoreText"
Step 32
Open the CollectCoins.cs and add these new lines of code into the script.
Step 33
Here's what the new code does. This is actually very similar to our keysRemaining.cs.
Check Your Work
Step 34
Save all scripts. Click play and see if our score works when we collect coins!
Step 35
You are finished with this lesson! Click on submit and go to the next lesson on Game Mechanics Challenges.
Scripting: UI Variables Info


MVCode Clubs

Created By

Nepgear tina (instructor)


Unity Advanced

Access Level


For Teachers and Schools

Teach coding to your students with MVCode Teach

MVCode offers an integrated product designed to teach coding to students.

Learn more about MVCode Teach