Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Feb 01, 2018 at 08:32 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Juancook · Apr 20, 2016 at 10:36 PM · texttime

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by TBruce · Apr 21, 2016 at 12:17 AM

@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;
             }
         }
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Juancook · Apr 21, 2016 at 02:30 AM 0
Share

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

Answers Answers and Comments

56 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges