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 Jarrod.F · Nov 08, 2013 at 11:44 PM · timer-script

How do I make time restart when I hit Zero? C#

 using UnityEngine;
 using System.Collections;
 
 public class PlayerTime : MonoBehaviour 
 {
 private float startTime = 5;
 private float restSeconds = 0;
 private int roundedRestSeconds = 0;
 private float displaySeconds = 0;
 private float displayMinutes = 0;
 public int CountDownSeconds = 120;
 private float Timeleft = 0;
 public Transform spawnPoin;
 public GameObject player;    
     
    string timetext = "";
  
     void Start () 
     {
  
         startTime = Time.deltaTime;
         
     }
     void ResetTimer ()
     {
         
        Timeleft = Time.time-startTime;
        restSeconds = CountDownSeconds-(Timeleft);
        roundedRestSeconds = Mathf.CeilToInt(restSeconds);
        displaySeconds = roundedRestSeconds % 60;
        displayMinutes = (roundedRestSeconds / 60)%60;
        timetext = (displayMinutes.ToString() + ":");
  
        if (displaySeconds > 9)
         {
            timetext = timetext + displaySeconds.ToString();
         }
        else 
         {
            timetext = timetext + "0" + displaySeconds.ToString();
         }
         
     }
   void OnGUI () {
  
        startTime = 0;
        Timeleft = Time.time-startTime;
        restSeconds = CountDownSeconds-(Timeleft);
        roundedRestSeconds = Mathf.CeilToInt(restSeconds);
        displaySeconds = roundedRestSeconds % 60;
        displayMinutes = (roundedRestSeconds / 60)%60;
        timetext = (displayMinutes.ToString() + ":");
  
        if (displaySeconds > 9)
         {
            timetext = timetext + displaySeconds.ToString();
         }
        else 
         {
            timetext = timetext + "0" + displaySeconds.ToString();
         }
         if (displaySeconds<= 0)
         {
             player.transform.position = spawnPoin.position;
             ResetTimer();
         }
         
        GUI.Label(new Rect(10, 10, 200, 40), timetext);
         }
     
       void ResetTime()
     {
         if(roundedRestSeconds == 0)
         {
             ResetTimer();
             
         }
     }
 }
Comment
Add comment · Show 2
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 tanoshimi · Nov 08, 2013 at 11:57 PM 0
Share
  • for including some code, but as it's uncommented and you have written no text in your question, its still pretty impossible to help you. What is this code meant to do? $$anonymous$$ost of it looks like it could be replaced with a single String.Format()

There's no variable in your code called "time" so what do you want to restart? startTime? Timeleft? Time.time?

And when you hit Zero? The zero key on the keyboard? Or when some counter hits zero? What counter?

avatar image Jarrod.F · Nov 09, 2013 at 12:33 AM 0
Share

I'm Sorry, I'll comment my work.

0 Replies

· Add your reply
  • Sort: 

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

Multiple Cars not working 1 Answer

help my unity wont start up when i try every thing but the game options gos white then when i go back to it later the white turns black 0 Answers

How to make FPS trailer? 1 Answer

How to have animation play correctly 1 Answer

GameObject' does not contain a definition for `Find' 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