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 /
avatar image
0
Question by WesterlyCarrot9 · Sep 13, 2013 at 12:38 AM · javascriptguitriggertimer

How to see minutes in the timer's box?

Greetings! So i used a simple timer script that i found on the internet and combined it with some GUI and a trigger that i need for my game. My question is how could i make it so that i see minutes and seconds in the GUI Box? Right now, when the player enters the trigger and the GUI Box spawns, i can see numbers running like hell :P Thanks!

 var showGUI : boolean = false;
 var myTimer : float = 20;
 var player : GameObject;
 
 function OnTriggerEnter(other:Collider){
     if(other.gameObject.name == "Player"){
         showGUI = true;
         Debug.Log("Trigger Enter");
     }    
 }    
 
 function OnGUI(){
     if(showGUI){
         if(myTimer > 0){
             GUI.Box(new Rect(20,50,70,50), myTimer + "");
             myTimer -= Time.deltaTime;
         }
         if(myTimer <= 0){
             GUI.Box(new Rect(20,20,100,50), "GAME OVER");
         }    
     }    
 }
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

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Paparakas · Sep 13, 2013 at 01:14 AM

Try doing

 float myTimer = 70;
 float TimeLeftInSeconds = 0;
 float Minutes = 0;
 float Seconds = 0;
 string message = string.Empty;
 function OnGUI(){
     if(showGUI){
        if(myTimer > 0){
         TimeLeftInSeconds = myTimer -= Time.deltaTime;
         if((TimeLeftInSeconds / 60) >= 1)
         {
             Minutes = Mathf.Floor((TimeLeftInSeconds / 60));
             Seconds = TimeLeftInSeconds - (Minutes * 60);
             message = "Minutes: " + Minutes + " Seconds: " + Seconds;
         }else{
             Seconds = TimeLeftInSeconds;    
             message = " Seconds: " + Seconds;
         }
          GUI.Box(new Rect(20,50,70,50), message);
        }
        if(myTimer <= 0){
          GUI.Box(new Rect(20,20,100,50), "GAME OVER");
        } 
     }  
 }

If you want to round up the seconds you can do Seconds = Mathf.Round(Seconds);

Comment
Add comment · Show 3 · 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 WesterlyCarrot9 · Sep 13, 2013 at 01:37 AM 0
Share

Never$$anonymous$$d i found it :) Thanks for your reply anyway :) Below is what i did and it works perfectly now like i want it.

 var showGUI : boolean = false;
 var myTimer : float = 200;
 var player : GameObject;
 var $$anonymous$$utes : int;
 var seconds : int;
 
 function OnTriggerEnter(other:Collider){
     if(other.gameObject.name == "Player"){
         showGUI = true;
         Debug.Log("Trigger Enter");
     }    
 }    
 
 function OnGUI(){
     if(showGUI){
         if(myTimer > 0){
             GUI.Box(new Rect(20,50,70,50), "" + $$anonymous$$utes + " : " + seconds + "");
             myTimer -= Time.deltaTime;
             $$anonymous$$utes = myTimer / 60;
             seconds = myTimer % 60;
             textTime = String.Format ("{0:00}:{1:00}", $$anonymous$$utes, seconds);
         }
         if(myTimer <= 0){
             GUI.Box(new Rect(20,20,100,50), "GA$$anonymous$$E OVER");
         }    
     }    
 }
avatar image Paparakas · Sep 13, 2013 at 01:54 AM 1
Share

Interesting, I never knew about the % operator. It makes things a lot easier. Guess I learned something today.

avatar image clunk47 · Sep 13, 2013 at 03:03 PM 0
Share

Be cool, vote up the answer you have accepted ;)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Showing text near object 1 Answer

How Would I Make A GUI Label Fade After A Certain Amount Of Time? 1 Answer

Setting Scroll View Width GUILayout 1 Answer

I get a weird error when i try to use GUISkin on a timer 1 Answer

Need help with game points and trigger 2 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