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 TRAUMA · Mar 31, 2012 at 01:24 AM · timerjava

Simple Timer

Hi there, I'm quite new to this and it is probably really easy to figure out, and it's a tiny thing but it's bugging me. I have a simple timer for my game, counting down from 1:15 to 0. When the counter reaches a number range of 0 - 9, meaning like '1 : 09' or '0 : 09' it will simply display it as '1 : 9' or '0 : 9', and I would like it as '1 :09' or '0:09'.

Here's my code, just going chuck in the whole thing, the timer code is in bold.

//WinZone And Timer Script

//Time Limit for this level var totalTime = 75; var DeathLayer : GameObject;

//Some Variables we need to declare var timeLeft = totalTime; private var Display; private var LoadingSequence: boolean = false; private var RunTimer: boolean = true; private var OutOfTime: boolean = false;

function Update() { //If we're not in the WinZone, run the timer if (RunTimer ==true) { var thisSecond; var lastSecond =0; timeLeft = totalTime - Time.timeSinceLevelLoad; if (timeLeft < 1) { OutOfTime =true; }

 }
 

//constrain time limit minimum timeLeft = Mathf.Max (0, timeLeft);

//Run a function that converts the time layout from a float to Minutes and Seconds TimerLayout (timeLeft);

}

//Convert time to minutes and seconds function TimerLayout(time){ var Clock : int = time; var minutes : int = Clock / 60; var seconds : int = Clock % 60;

//Convert output of minutes and seconds to strings and add a ":" in between. Display = minutes.ToString () + " : "; Display = Display + seconds.ToString (); }

//if player enters the WinZone function OnTriggerEnter (other : Collider){ Destroy (DeathLayer); //Show the endlevel gui elements LoadingSequence = true; //stop the timer RunTimer = false; } var Style_1 : GUIStyle; var Style_2 : GUIStyle; var Style_3 : GUIStyle; var Style_4 : GUIStyle; var Style_5 : GUIStyle; var Button_1 : GUIStyle; var Button_2 : GUIStyle; var Button_3 : GUIStyle; var Button_4 : GUIStyle; function OnGUI () { //show timer

 if (LoadingSequence ==false)
 {
 GUI.Box (Rect ((Screen.width - Screen.width + 10), (Screen.height - Screen.height + 10), 200, 40), "TIME = " + Display, Style_4);
 
     if (timeLeft == 10)
         {
             
             Style_4 = Style_5;
         }
 }
 
 //if player is in the WinZone, show differnt gui elements
 if (LoadingSequence ==true && OutOfTime == false)
 {
 GUI.Box (Rect ((Screen.width - 360) / 2, (Screen.height - -300) / 2, 370, 120), "CONGRATS! LEVEL ACCOMPLISHED!", Style_1);
 GUI.Label (Rect ((Screen.width - 125) / 2, (Screen.height - -390) / 2, 140, 100), "TIME = " + Display, Style_2);
     if (GUI.Button (Rect ((Screen.width - 320) / 2, (Screen.height - -460) / 2, 200, 28), "RETURN TO LEVEL ROOM", Button_1))
         {
         Application.LoadLevel("Level_Room");
         }    
         
     if (GUI.Button (Rect ((Screen.width - -115) / 2, (Screen.height - -460) / 2, 111, 28), "NEXT LEVEL", Button_2))
         {
         Application.LoadLevel("Level_2");
         }
 }

 if (OutOfTime == true)
 {    
 GUI.Box (Rect ((Screen.width - 240) / 2, (Screen.height - -300) / 2, 240, 100), "OUT OF TIME!", Style_3);
 Screen.lockCursor = false;
     if (GUI.Button (Rect ((Screen.width - 80) / 2, (Screen.height - -370) / 2, 90, 20), "TRY AGAIN?", Button_3))
     {
     Application.LoadLevel(Application.loadedLevel);
     }
     if (GUI.Button (Rect ((Screen.width - 40) / 2, (Screen.height - -440) / 2, 50, 20), "QUIT?", Button_4))
     {
     Application.LoadLevel("Level_Room");
     }
 }

}

And yes, I know, it's real sloppy, just getting the basics there then going to tidy it up. Thank's in advance for the help.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by TRAUMA · Mar 31, 2012 at 12:03 PM

And half my code hasn't come out as a 'sample'. Sorry guys, it's pretty unreadable, any help from what can be made from it would be appreciated, thank's.

Comment
Add comment · 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
0

Answer by dvidunis · Mar 31, 2012 at 09:49 AM

is this simple? simple is this:

var timer:float = 0;

function Start(){ while(true){ yield WaitForSeconds(0.1); timer += 0.1; }}

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Disply ad not calling in Device 0 Answers

Script help please 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Trying to Create a Powerup that lasts a certain amount of time 1 Answer


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