Scripting: UI Variables

Step 1
In this lesson we'll learn how to add variables to the UI!
Temp1547070177
Step 2
Load/Open your project, you should be able to shoot objects/particles from the previous lesson.
Temp1546466555
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.
Temp1546564653
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.
Temp1546564923
Step 6
Open your Shoot.cs Script and we'll try to connect the UI to this script!
Temp1546565119
Step 7
Add these lines of code to your Shoot.cs script.
Temp1546652844
Information
Step 8
What this code does.
Temp1546653071
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.
Temp1546653456
Challenge
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
Planning
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"
Temp1547065320
Step 13
Type "Keys Remaining: " into the text box of this UI Text object.
Temp1547065743
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.
Temp1547066246
Information
Step 16
(You may need to click on this to enlarge and read it) Let's see what this new code does.
Temp1547066643
Step 17
Go back to your Canvas object in the Hierarchy, select it.
Temp1547066759
Step 18
Give this a tag called "KeysText"
Temp1547066798
Step 19
Open your EnableNext.cs script and add this new code into it.
Temp1547066868
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.
Temp1547067104
Information
Step 21
Go back to Unity. If your keys were hidden before, enable them all so we can see them all.
Temp1547067164
Planning
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"
Temp1547068730
Step 28
Just like with our other text, in the text box, type "Score: "
Temp1547068859
Step 29
Position your score text on your Canvas, I put mines on the top right of the screen.
Temp1547068901
Information
Step 30
Important!! Make sure all text boxes have empty space on the right.
Temp1547068960

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"
Temp1547069182
Step 32
Open the CollectCoins.cs and add these new lines of code into the script.
Temp1547069585
Information
Step 33
Here's what the new code does. This is actually very similar to our keysRemaining.cs.
Temp1547069888
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

Account

MVCode Clubs

Created By

Nepgear tina (instructor)

Course:

Unity Advanced

Access Level

premium

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