In this recipe, we are going to create a timer that displays the time (as a number) on the screen. This may be a useful element when attempting to inform the player how much time has passed during an objective. In some cases, if the player is aware of how much time he is taking with a task, it may encourage him to be more efficient and considerate about his choices throughout the gameplay. To create a timer, we will use the Text (Script) component as well as develop a script to implement the logic of the timer.
time
, even if this value will be replaced by the script. This is so that we have a better approximation of how it will look. This will help us to better gauge whether its placement will be effective or will inhibit or distract the player during gameplay.using UnityEngine.UI;
statement at the beginning of the script. This allows us to handle the UI elements within the script.private
. So, we can write this:private Text uiText; private floattime;
uiText
variable in the Start()
function. In fact, this is the ideal function for assigning the initial value to our variables, since it is called when the script runs for the first time in the game environment. Therefore, we can assign the reference to the Text (Script) component to the variable just before the timer starts, in the following way:void Start () { uiText = this.GetComponent<Text> (); }
Update()
function:void Update () { time += Time.deltaTime; uiText.text = "<color=blue>Time</color>: " + time.ToString("F2"); }
First of all, we created a new UI text, and then adjusted it to suit our needs. We changed Font, Font Size, and also Color. In addition, we ensured that Rich Text was checked. In this way, it is possible to use different colors in the text of the timer. Then, we wrote a script to handle the timer.
In the script, there are two private variables: uiText
and time
. The uiText
variable stores the reference to the Text (Script) component, so we can get access to it and modify its text. The time
variable stores the amount of time that has passed since the timer started.
In the Start()
function, we assigned the reference to the Text (Script) component, which is attached to the same game object of this script, to the uiText
variable by calling the this.GetComponent<Text>()
function.
Keeping in mind that the Update()
function is called at every frame, we added Time.deltaTime
, which is the time that has elapsed from the last frame to the time
variable. We did this to accumulate the time that has passed since this script/timer started. In the second line, we updated the text
variable on the Text (Script) component, which we can have access to through the uiText
variable, with the new time value. Since we don't want all the decimal points, we have to change it to float
by calling ToString("F2")
on it.
The next section will teach us how to retrieve the value of our variable.
get
function, refer to the Implementing a score counter recipe in Chapter 2, Implementing Counters and Health Bars, inside the There's more... section13.58.113.193