Hello i’m new at using unity and i’m making a game, please I want to know how to get the remaining value of the timer and get it as a score. I have a treasure chest that when the player collides with it a GUI will appear and there’s a text (the question) and 4 buttons (multiple choices) with a timer of 10 seconds that counts down to 0. (ex. If the player answered correct with a remaining time of 5 he will get a score of 5)
Assuming you already have the script to show the UI, and you’re using C# and uGUI (the new gui). Also you will want to handle the case where time variable will be 0.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class TimerScore : MonoBehaviour
{
public Text text;
public int startTime; // we will count down from this value
private int time;
public void StartTimer()
{
time = startTime;
StartCoroutine("timer");
}
public void StopTimer()
{
StopCoroutine("timer");
text.text = "Score: " + time;
}
private IEnumerator timer()
{
while(time > 0)
{
time--;
yield return new WaitForSeconds(1f);
}
}
}