This is how you create a score that is linked to the movement of the player.
For a text UI in Unity, create a new script called Score. First you build a reference to the player:
public transform player;
Transform specifies position, scaling, and so on.
Under Update we add: player.position.z;
It is about the position of the player on the Z-axis. This should be 0.
The first part refers to the player from the score.
Now the reference to the UI follows.
For this you use:
using UnityEngine.UI;
The next required variable is:
public text
Text is used because the component responsible for the high score is the text.
Then you name the text with: scoreText;
The scoreText. can be inserted. with scoreText.text you tell the PC that the text is changing.
You set this with the player position: = player.position.z;
The player position must be converted into text. For this you also need a string function and add: .ToString ()
Drag the text component into the newly created field:
Now you insert a "0" in the brackets of the string component. This is telling the system that you only want whole numbers.
In order for Unity to optimize the pixels, you can also activate Pixel perfect.
The finished script looks like this:
Originally posted on 2020-06-07 12:19:00.