Lab 5 - Fruit Picker p3 - Updated

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Lab 5 – Fruit

Picker: part 3
INFO 4328 – Game Design & Development
(DIS/KICT)
Score Counter

• In the Hierarchy, right-click UI àText – TextMeshPro, ,


then if prompted click the button to Import TMP
Right-click Essentials.

• Rename the new object to “HighScore”, then zoom out to


see the canvas in Scene view (switch to 2D view and hit
Rename F).

• Change the Anchor Point so that it is anchored from the


top-left corner.
Change

• Readjust the text position.


Readjust
Score Counter

Change Change Change


Change the Change the Change the
text to High Canvas UI font size and
Score: 1000. Scale Mode to color to suit
Scale with the scene.
Screen Size.
Duplicate HighScore and
rename it to ScoreCounter.

Score Counter
Set the anchor to top-right and
readjust the text position, and
the text.
Adding
Points to
Each
Caught
Fruit
When a collision happens between the fruit and the basket, the score will
Be be updated.

Open Open the FruitPicker script and at the top, add “using TMPro;”.

Declare a new public TextMeshProUGUI scoreText, then assign that variable


Declare in the inspector.

Create Create a new private int score variable and initialize it in Start() as score = 0;
Adding Points to Each Caught Fruit

• Create a new private void UpdateScore


method that requires one int
scoreToAdd parameter.
• In UpdateScore(), increment the score
by adding score += scoreToAdd;
• Call UpdateScore(1) in the Update()
function to test.
Adding Points to Each
Caught Fruit

In FruitPicker.cs, make sure the


UpdateScore method is set to public.

Open the Basket script, create a reference


to private FruitPicker fruitPickerManager;

Initialize FruitPicker object in Start() using


the Find() method.

In OnCollisionEnter function, call the


UpdateScore method.
Collision Between Fruit
and Ground
• After adding the collider box to the Fruit,
you’ll notice that it doesn’t go through the
ground anymore, since the ground has its own
collider.
• This will cause an issue because whenever the
box collides with the fruit already on the
ground, points will be added to the score.
• To fix this, add a new layer called Ground and
assign it to the Ground object.
• Make sure to uncheck the collision between
Fruit & Ground in the Layer Collision matrix.
Adding A High Score

Create • Create a new script called Highscore and attach it to the


High Score object.

Add • Add using TMPro; in the script.

Create • Create a new static variable to hold the highscore: static


public int highscore = 20; and public TextMeshProUGUI
highscoreText; (assign the variable in the Inspector)

Add • Add the following code in Update.


Open the FruitPicker script.
Updating the
High Score Inside Update(), add the following
if statement.
Preserving the
Highscore
• To preserve the Highscore even after the
game is stopped, we need to use
PlayerPrefs.
• PlayerPrefs variables store information
from Unity scripts on the computer so that
the information can be recalled later and
isn’t destroyed when playback stops.
• PlayerPrefs also work across the Unity
editors, compiled builds, and WebGL builds,
so the high score you get in one will carry
over to the others, as long as they're run on
the same machine.
Preserving the Highscore

• Open the Highscore


script.
Open

• Add an Awake
method in it.
Add
• Also, add the
following code in
Add Update.
Some Explanation…
Awake() is a built-in Unity MonoBehaviour method
(like Start() or Update()) that happens when this so Awake()always occurs before Start().
instance of the HighScore class is first created.

PlayerPrefs is a dictionary of values that are referenced through keys (that is, unique strings). In this
case, you're referencing the key HighScore.

This usage of PlayerPrefs enables the FruitPicker high score to be remembered on this machine, and
the high score will survive stopping playback, quitting Unity, and even restarting your computer.

You might also like