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!”;
}
}
}

@Juancook

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.

  1. You want a timer to start at some unspecified time (it could be in the Start() function?)
  2. 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;
            }
        }
    }
}