Now that our UI system is set up, we need to actually call the function of the last section when the player collects a cake. To achieve this, we need to open the CollectableCake script from Chapter 3, Let's Make Some Prefabs. If you recall, we left a To-Do which was to link the moment the player collects a cake with the UI.
In fact, we start by creating another variable (settable in the Inspector), and this time public, to store the value of the single cake. As a result, you will be able to change the value of every single cake. This is useful in case we want to add other collectable cakes, maybe that are more rare, which scores more for the player. Here is the variable declaration:
//Stores the value of the cake in terms of Player's score
public int cakeValue = 1;
Then, we need to substitute the TODO line of code with the following, so we actually call the UI script we wrote before:
if (other.tag == "Player") {
//If so, increase the number of cakes the player has collected
FindObjectOfType<UI_Score>().IncreaseScore(cakeValue);
//[…]
}
And that's it. Now, let's create a Game Over Screen before we tweak the last settings in the Inspector and finally get our first game finished!