Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Mar 08, 2015 at 11:52 PM by KnightRiderGuy for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by KnightRiderGuy · Feb 20, 2015 at 03:43 PM · uitimer-scripttimersuifont

How Can I make a Timer With The New UI System?

I have this Java script timer which works fine but I want to try and make one like this using the new U.I. system in C#? This is the one I currently have in Java.

 #pragma strict
  private var time : float;
  var textTime : String;
  var timerOn : boolean;
  var buttonText : String;
  var beep1 : AudioClip;
  var beep2 : AudioClip;
  var buttonTexture: Texture2D;
   
  function Start() {
      timerOn = false;
      buttonText = "Start";
  }
   
  function Update(){
      if(timerOn)
          time += Time.deltaTime;
  }
  
  
   
  function OnGUI () {
       var guiTime = time;
       
       var minutes : int = guiTime / 60; //Divide the guiTime by sixty to get the minutes.
       var seconds : int = guiTime % 60;//Use the euclidean division for the seconds.
       var fraction : int = (guiTime * 100) % 100;
    
       textTime = String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction); 
       //text.Time is the time that will be displayed.
       GetComponent(GUIText).text = textTime;
   }
   
   public function TimerOnOff()
  {
      // do your timer on/off stuff
      timerOn = !timerOn;
           if(timerOn) buttonText = "Stop";
           else buttonText = "Start";
           audio.PlayOneShot(beep1);
  }
  
   public function TimerReset()
  {
      // do your timer reset stuff
      time = 0;
          audio.PlayOneShot(beep2);
  }
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
12
Best Answer

Answer by Neamtzu · Feb 20, 2015 at 05:29 PM

Hi,

Create a new script from this code:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class timer : MonoBehaviour {
     public Text timerLabel;
 
     private float time;
 
     void Update() {
         time += Time.deltaTime;
 
         var minutes = time / 60; //Divide the guiTime by sixty to get the minutes.
         var seconds = time % 60;//Use the euclidean division for the seconds.
         var fraction = (time * 100) % 100;
 
         //update the label value
         timerLabel.text = string.Format ("{0:00} : {1:00} : {2:000}", minutes, seconds, fraction);
     }
 }

Create a new GameObject, attach the script you created. Create a label. Select the first object you created and in inspector drag and drop the label in the timerLabel variable.

Comment
Add comment · Show 13 · 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 KnightRiderGuy · Feb 20, 2015 at 08:03 PM 0
Share

Thanks Neamtzu I'll give this a shot and see if it works out.

avatar image KnightRiderGuy · Feb 20, 2015 at 09:19 PM 0
Share

How do you create a label?

avatar image KnightRiderGuy · Feb 20, 2015 at 10:14 PM 0
Share

This needs to be explained better, I tried doing what you said but I think I am missing something. I think also that you are asking me to use part of the old OnGUI stuff which is what I am trying to steer clear of.?

avatar image Neamtzu · Feb 20, 2015 at 10:20 PM 0
Share

No, if you're using Unity 4.6x you can create a text object (text, not label, sorry) GameObject->UI->Text. I'll edit the first answer.

avatar image Neamtzu · Feb 20, 2015 at 10:22 PM 0
Share

So, your timerLabel variabile should have the type Text. Everything else should be ok now. Just create an text object and drag it in inpector in the timerLabel slot.

Show more comments

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Unity 5 - Time counter Up script (millisecond precision) UI 1 Answer

Clock Script From Java to C# Help 1 Answer

CanvasRenderer text is behind sprites 4 Answers

How Too Scrolling UI Text Message Centre 1 Answer

How to make a stopwatch that restarts every time the player dies and saves it as a highscore 3 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