how to get remaining time in a count down timer as a score?
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)
Add your current code, and what you have already tried please.
Answer by legion_44 · Aug 29, 2015 at 02:47 PM
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);
}
}
}
Thanks for the help! After hours and hours of trial and error to convert this to javascript and I was able to create one. What im missing is the StopTime() function to get time remaining or current time when I press a button.
Here's the script that I made.
var toggleGUI : boolean;
var timer : float = 10.0f;
var getTime : int = 0;
var time : boolean;
function Update ()
{
if (time == true)
{
timer -= Time.deltaTime;
}
}
function OnTriggerEnter (other : Collider)
{
time = true;
toggleGUI = true;
}
function OnGUI () {
if (toggleGUI == true){
GUI.Box (Rect (0, 0, Screen.width, Screen.height), "Question #1");
GUI.Label (Rect(Screen.width/2-100/2, 50, 150, 20), "Ano mas malaki?");
GUI.Label (Rect(Screen.width/2-100/2, 25, 150, 20), "Remaining Time: " + timer.ToString("0"));
if (GUI.Button (Rect (Screen.width/2-100/2,130,120,20), "A. Correct Answer"))
{
correct();
}
if (GUI.Button (Rect (Screen.width/2-100/2,160,120,20), "B. Wrong Answer"))
{
//game over
}
if (GUI.Button (Rect (Screen.width/2-100/2,190,120,20), "C. Wrong Answer"))
{
//game over
}
if (GUI.Button (Rect (Screen.width/2-100/2,220,120,20), "D. Wrong Answer"))
{
//game over
}
}
}
function correct()
{
TimerStop();
Game$$anonymous$$aster.currentScore += getTime * 100;
Destroy(gameObject);
}
function TimerStop()
{
getTime = timer - Time.deltaTime;
}
Your answer
Follow this Question
Related Questions
How do I switch scenes with countdown timer and Score at the same time 0 Answers
How do I stop a scene from switching from switch before the Countdown timer reaches zero 2 Answers
How do add scenes on the Countdown Timer 0 Answers
How would add an scene on the Countdown Timer 1 Answer
How do I call an if and else statement on a Countdown timer Script 0 Answers