• 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
Question by Toy · Feb 28, 2012 at 09:25 PM · timerdeathsecondsminutes

Making a timer

Hi there i made a simple timer calculating the time im playing.

How can I change my seconds to minutes when it reaches 60 secs.

How can I kill my avatar when my timer reaches 5 min.

Here is what I did so far.

using UnityEngine; using System.Collections;

public class AvatarTimer : MonoBehaviour

{

     public GUIText timer;
     public float myTimer = 0.0f;

    void Update () 
    {
     myTimer+=Time.deltaTime;
     timer.text = "Time :" + (int)myTimer;
     
            if (myTimer >= 300.0f)
            {
             Debug.Log("GAME OVER");
            }
         
     }

}

THX

Comment

People who like this

0 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 J-R-Wood · Feb 15, 2014 at 04:19 PM 0
Share

how would I change timer.text = "Time :" + (int)myTimer; to JavaScript I already did the rest.

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by JinxM · Feb 28, 2012 at 11:16 PM

So it looks like the Debug.Log line should work correctly, right? Add in the same position a Destroy() function with a reference to your Avatar GameObject and it should work.

e.g. if the AvatarTimer script is on the Avatar GameObject:

 if (myTimer >= 300.0f)
 {
     Debug.Log("GAME OVER");
     Destroy(gameObject);
 }

Changing seconds to minutes is slightly more complicated and requires some formatting with ToString():

 int minutes = (int)myTimer / 60;
 int seconds = (int)myTimer % 60;
 timer.text = "Time: " + minutes.ToString() + ":" + seconds.ToString("00");

Alternately, there's some wacky stuff you can do with System.DateTime and System.TimeSpan to format it automatically instead of dividing and modding by 60, but this is a little easier to understand.

Comment
Toy
Zanyblax
spinnerbox

People who like this

3 Show 2 · 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 Toy · Feb 28, 2012 at 11:23 PM 0
Share

ok thx Ill try

avatar image Toy · Feb 29, 2012 at 01:06 AM 0
Share

Its perfect thank you

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

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

How to stop a Countdown Timer? 1 Answer

making a timer (00:00) minutes and seconds 10 Answers

Countdown Timer Help About putting 0's 1 Answer

Turn seconds into minutes and seconds MM:SS 1 Answer

I'm trying to get and store max elapsed time in min:sec format. Also, the best time for Highscore. Not working! 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