The question is answered, right answer was accepted
How can I do a timer with this script?
using UnityEngine; using UnityEngine.UI; using System.Collections;
public class PlayerController : MonoBehaviour{
public float fastSpeed = .7f;
public float moveSpeed = 10f;
public float turnSpeed = 50f;
public Text timer;
public Text winText;
public Text countText;
private int count;
private float time;
void Start()
{
count = 0;
winText.text = "";
SetCountText();
}
void Update()
{
if (Input.GetKey(KeyCode.UpArrow))
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
if (Input.GetKey(KeyCode.DownArrow))
transform.Translate(-Vector3.forward * moveSpeed * Time.deltaTime);
if (Input.GetKey(KeyCode.LeftArrow))
transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);
if (Input.GetKey(KeyCode.RightArrow))
transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
if (Input.GetKey(KeyCode.Space))
transform.Translate(Vector3.forward * fastSpeed * Time.deltaTime);
}
void OnTriggerEnter(Collider other) { if (other.gameObject.CompareTag("Pick Up")) { other.gameObject.SetActive(false); count = count + 1; SetCountText(); } }
void SetCountText() { countText.text = "" + count.ToString(); if (count >= 1) { winText.text = "FELICITACIONES!"; } } }
Answer by TBruce · Apr 21, 2016 at 12:17 AM
Again not really enough information. I am going to have to make some assumptions based on what you have already asked and your sample code.
You want a timer to start at some unspecified time (it could be in the Start() function?)
When you set winText.text to "FELICITACIONES!" you want to end the timer count up and display how long it took.
The code below will display the timer upon calling the StartTimer() function. It allows you to display the elapsed time in just seconts or in seconds/minutes. It will automatically stop when "count" is >= 1.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public float fastSpeed = .7f;
public float moveSpeed = 10f;
public float turnSpeed = 50f;
public Text timer;
public Text winText;
public Text countText;
public bool displayMinutes = false;
private int count;
private float time;
void Start()
{
count = 0;
winText.text = "";
SetCountText();
StartTimer();
}
void Update()
{
if (Input.GetKey(KeyCode.UpArrow))
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
else if (Input.GetKey(KeyCode.DownArrow))
transform.Translate(-Vector3.forward * moveSpeed * Time.deltaTime);
else if (Input.GetKey(KeyCode.LeftArrow))
transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);
else if (Input.GetKey(KeyCode.RightArrow))
transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
else if (Input.GetKey(KeyCode.Space))
transform.Translate(Vector3.forward * fastSpeed * Time.deltaTime);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
other.gameObject.SetActive(false);
count = count + 1;
SetCountText();
}
}
void SetCountText()
{
countText.text = "" + count.ToString();
if (count >= 1)
{
winText.text = "FELICITACIONES!";
}
}
void StartTimer()
{
if (timer != null)
{
time = Time.time;
if (!displayMinutes)
timer.text = "Time Elapsed: 000";
else
timer.text = "Time Elapsed: 00:00";
InvokeRepeating("UpdateTimer", 1.0f, 1.0f);
}
}
void UpdateTimer()
{
if ((count < 1) && (timer != null))
{
float elapsedTime = Time.time - time;
if (!displayMinutes)
{
// this line displays seconds elapsed only
string seconds = elapsedTime.ToString("000");
timer.text = "Time Elapsed: " + seconds;
}
else
{
// if you want to display minutes and seconds use the 3 lines below instead
string minutes = Mathf.Floor(elapsedTime / 60).ToString("00");
string seconds = (elapsedTime % 60).ToString("00");
timer.text = "Time Elapsed: " + minutes + ":" + seconds;
}
}
}
}
Sorry for the question , I would like to add a timer count up in C# and finish to count when the winText appear but also I want to save the time in the level and I don´t how can I start in the script
Follow this Question
Related Questions
How to export in a text file float numbers generated in a list 1 Answer
What is the best way to search a text clock UI for certain times 0 Answers
Hide (not delete) text after given time..? 1 Answer
Type out text in sync with audioclip 0 Answers
how to convert youtube videos duration time to seconds 0 Answers