Programming the scoring system

Finally, we get to get our hands dirty with a bit of code. Don't worry, it will be simple.
As usual, let's start by creating a new C# script, name it UI_Score, and open it up in your editor of choice.
First of all, if we want to use the UI system within a script, we need to import it in the context of our script. We can achieve that with a using statement at the beginning of our script, as highlighted here:


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UI_Score : MonoBehaviour {
//[…]
}

Once we have done this, we are ready to use all the Unity UI components in our script. In fact, the first variable we need is a reference to the Text Component we created in the previous section:

//Reference to the Text component, set in the Start() function
private Text uiText;

Then, we need a variable to store the actual value of the score, and another one that stores the number of points required to pass to the next level. Please note that in this case, we used the [SerializeField] attribute before the private variable to still make it editable from the Inspector:

//Current score of the player
private int score;
//Points required to the next level (set in the Inspector)
[SerializeField]
private int pointsToNextLevel;

In a similar fashion, we need to get the reference to the Game Over Screen, which we will create later. Thus, we can write:

//Reference to the game over screen GameObject (set in the Inspector)
[SerializeField]
private GameObject gameOverScreen;

Then, we need to get the reference to the Text Component in the Start() functions, therefore it becomes:

// Use this for initialization
void Start () {
//Get a reference to the Text component
uiText = GetComponent<Text>();
}

Finally, we need to create a function that takes, in an input, the number of points to add to the player's score and visualize it in the UI. The function should also check whether the new score is equal to or greater than the points needed for the next level, and if so, it shows our Game Over Screen and disables the player input. As a result, this is the function we need:

public void IncreaseScore(int points) {
//Increase the points
score += points;
//Check if the player has collected all the points to the next level
if(score >= pointsToNextLevel) {
//If so, show the game over screen
gameOverScreen.SetActive(true);
//Disable the player controller, so the player cannot move while the Gameover screen is on
FindObjectOfType<PlayerController>().enabled = false;
}
//Update the Score count
uiText.text = "Score: " + score.ToString();
}

And with this, we can save the script, since we have finished with this script. However, we still need to call this last function every time the player collects a cake. But before we do that, don't forget to attach this script to the ScoreLabel!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset