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
2
Question by Dreatix · Jun 23, 2012 at 08:32 PM · gameobjecttimetimer

Unity3D Timer.

Hi,I have tried all sort of ways to make timer.NOTHNG.I need timer which from a particular time goes to zero,when it goes to the end-0,game ends (shows text: time over...) And then i can restart game: pressing restart.And when i finish round (''first'' level): TIMER STOPS.Help please.

Comment
Add comment · Show 1
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 Fattie · Jun 23, 2012 at 08:54 PM 1
Share

learn about the INVO$$anonymous$$E command, enjoy

1 Reply

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

Answer by aldonaletto · Jun 23, 2012 at 09:02 PM

You can attach this script to a GUIText, which can be used to show the remaining time and the message Time Over - create a GUIText, position it and add the script below:

var timer: float = 300; // set duration time in seconds in the Inspector

function Update(){ timer -= Time.deltaTime; if (timer > 0){ guiText.text = timer.ToString("F0"); } else { guiText.text = "TIME OVER\nPress X to restart"; if (Input.GetKeyDown("x")){ // reload the same level Application.LoadLevel(Application.loadedLevel); } } }

Comment
Add comment · Show 11 · 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 Dreatix · Jun 24, 2012 at 09:07 AM 0
Share

Its realy basic,i want something like that what i wrote,I dont have any idea how to do that.

avatar image AlucardJay · Jun 24, 2012 at 09:28 AM 2
Share

Aldo's answer is what you asked for. Read through and see what is happening (I have spaced the lines and added your question as comments) :

 var timer: float = 300; // set duration time in seconds in the Inspector

 function Update()
 {
     timer -= Time.deltaTime; // I need timer which from a particular time goes to zero
     
     if (timer > 0)
     {
         guiText.text = timer.ToString("F0");
     } 
     else // timer is <= 0
     {
         guiText.text = "TI$$anonymous$$E OVER\nPress X to restart"; // when it goes to the end-0,game ends (shows text: time over...) 
         
         if (Input.Get$$anonymous$$eyDown("x")) // And then i can restart game: pressing restart.
         { 
             Application.LoadLevel(Application.loadedLevel); // reload the same level
         }
     }
 }
avatar image Dreatix · Jun 24, 2012 at 09:29 AM 0
Share

No thats not,game is not stopping,i need to press x then it will ONLY RESTART.

avatar image macrian Dreatix · Jun 17, 2016 at 07:20 AM 2
Share

Seriously. If you want people just to do it for you for free then go somewhere else. People here have explained to you what to do. Given you code and guidelines. If you want a "do it for me now, exactly as I want it" answer then go somewhere else

avatar image macrian macrian · Jun 17, 2016 at 07:21 AM 1
Share

Just realized that original question was 4 years ago. Still leaving the comment here to discourage people in the future to have this attitude

avatar image AlucardJay · Jun 24, 2012 at 09:38 AM 1
Share

you have to tell the timer to stop if the level has been finished. I have added a boolean and a condition to check if the level has been completed or not.

 var timer: float = 300; // set duration time in seconds in the Inspector
 var isFinishedLevel : boolean = false; // while this is false, timer counts down

 function Update()
 {
     if (!isFinishedLevel) // has the level been completed
     {
         timer -= Time.deltaTime; // I need timer which from a particular time goes to zero
     } 
     
     if (timer > 0)
     {
         guiText.text = timer.ToString("F0");
     } 
     else 
     {
         guiText.text = "TI$$anonymous$$E OVER\nPress X to restart"; // when it goes to the end-0,game ends (shows text: time over...) 
         
         if (Input.Get$$anonymous$$eyDown("x")) // And then i can restart game: pressing restart.
         { 
             Application.LoadLevel(Application.loadedLevel); // reload the same level
         }
     }
 }

How you use a timer is depending on your code, but the essential theory is there :

 // TI$$anonymous$$ER counting UP
 var timer : float = 0.0;
 var timer$$anonymous$$ax : float = 3.0;
 function Update()
 {
     timer += Time.deltaTime;
     
     if (timer >= timer$$anonymous$$ax)
     {
         Debug.Log("timer$$anonymous$$ax reached !");
         
         // reset timer
         timer = 0;
     }
 }
 
 // TI$$anonymous$$ER counting DOWN
 var timer : float = 0.0;
 var timer$$anonymous$$ax : float = 3.0;
 function Start()
 {
     timer = timer$$anonymous$$ax ;
 }
 function Update()
 {
     timer -= Time.deltaTime;
     
     if (timer < 0)
     {
         Debug.Log("timer Zero reached !");
         
         // reset timer
         timer = timer$$anonymous$$ax;
     }
 }
avatar image Dreatix · Jun 24, 2012 at 09:47 AM 0
Share

Awesome,it works,you are super,now can you please help me to make game freezes when time is reached to zero,and shows something like : you are lost and i can press restart.And please help me to make timer shows in $$anonymous$$utes,like 2:00 not 200,well $$anonymous$$utes and frozen game is important thing,Please help me at this.I really appreciate your help!

Show more comments

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

6 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Unity3D Game Time 1 Answer

How to tell if 2 blocks are next to each other in a 2d game? 1 Answer

Cant change GO material with multipli material 1 Answer

How do you save timer with PlayerPrefs? 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