Gamespace: Count & Win Text
I wanted a way to count up the number of items the player has collected in the Gamespace. Therefore I decided to add a count feature and a win text which informs the player that all items have been collected in the Gamespace.
Game View including count and win text |
The count feature adding up the number of collected items |
Win text displayed as all items have been collected |
The count and win text are both displayed on the screen as they are child objects of the canvas object which also holds the health bar.
The Render Mode has been set to Overlay so that both pieces of texts are overlaid on the screen and are separate from the Gamespace. For example, they aren't visible in the background of the screen and are clearly in the foreground.
Moreover, the UI scale mode in the Canvas Scaler has been set to 'Scale With Screen Size' meaning that if the screen is made bigger/smaller, the count and win text will re-adapt and fit with the screen size.
The inspector for the count text showing its position on the canvas.
The inspector for the win text, also displaying its position on the canvas.
Canvas Inspector |
The inspector for the count text showing its position on the canvas.
Count text Inspector |
The inspector for the win text, also displaying its position on the canvas.
The horizontal overflow had been set to overflow so that all of the text would be displayed on the screen.
The 'ScoreManager' script is attached to the 'Player' in its inspector.
In the Start() function, the variable 'scoreManager' is assigned to the gameObject with the tag 'Player'. The ScoreManager component would be received.
In the 'OnTriggerEnter' function, when the player has collided with an item, the functions 'IncrementScore' and 'WinScore' are called from the ScoreManager script so that the score could be increased by 1 and updated on the game view screen and the conditions to display the win text can be checked so that if the player has scored 20 points, the win text message is displayed on the game view screen.
Win Text Inspector |
The 'ScoreManager' script is attached to the 'Player' in its inspector.
The text type could only be used if I declared in the script that I was using 'UnityEngine.UI'.
The Text variables 'scoreText' and 'winText' were set to public so that the text could be edited in the inspector. The integer variable 'score' was set to private so that the value could not be changed outside of the script.
In the Start() function an empty space is assigned to the 'winText' variable so that it cannot be seen straight away by the player as I only wanted the win text displayed when the player had collected all items in the Gamespace.
The function 'IncrementScore' was used to increase the score and change the count text so that the new score was displayed to the player.
The function 'WinScore' uses an if statement that states that if the value assigned to the score variable is equal to 20 (the maximum items to collect in the Gamespace is equal to 20), the win text will be changed to display that all items had been collected.
ScoreManager script |
The pickUp script was updated to incorporate the score and win text feature.
ScoreManager was defined as its own type and was set to public so that it couldn't be edited outside of the script.In the Start() function, the variable 'scoreManager' is assigned to the gameObject with the tag 'Player'. The ScoreManager component would be received.
In the 'OnTriggerEnter' function, when the player has collided with an item, the functions 'IncrementScore' and 'WinScore' are called from the ScoreManager script so that the score could be increased by 1 and updated on the game view screen and the conditions to display the win text can be checked so that if the player has scored 20 points, the win text message is displayed on the game view screen.
Updated PickUp Script |
Comments