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 Jamie_h88 · Oct 06, 2013 at 08:20 AM · timecountertimer countdown

Timer not counting down in seconds

I have a counter that did work but now counts down from 100 in around 3 seconds. I need it to count down one very second.

 var score = 0;
 var timer = 100;
 var scoreText = "Score: 0";
 var levelToLoad : String;
  
 function Start()
 {
 
 }
  
  
 function Update()
 {
     
    timer = timer - Time.deltaTime;
    
    Debug.Log(timer);
    
    if (score < 1)
    {
            Application.LoadLevel(levelToLoad);
    }
  
 }
 
 function OnGUI () 
 {
      GUI.Label (Rect (10, 10, 100, 20), scoreText + score);
 }
  
 function addsometoscore()
 {
     score += 20;
 }
 
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
3
Best Answer

Answer by vexe · Oct 06, 2013 at 08:58 AM

Just tested it out, declare your timer variable explicitly as float and it should work:

 var timer : float = 100;
 void Update()
 {
    timer -= Time.deltaTime;
    print (timer);
 }

If you don't explicitly declare your variable as a float, it will default to System.Int32 which seems to be causing this fast countdown - To prove it:

 var timer = 100;
 void Start()
 {
    print (typeof(timer));
 }

If you were on C# you'd get an error right away telling you Cannot convert float to int - because:

 timer = timer - Time.deltaTime
 int   = int   - float
 int   = float?

There has to be a match when you assign a value to varible. Not sure how JS even allow this to happen.

Comment
Add comment · Show 4 · 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 Jamie_h88 · Oct 06, 2013 at 12:44 PM 0
Share

It worked to an effect. Only problem is that the whole number counts down per 1 sec. I'm now trying to convert this into a score and the problem reocoors.

avatar image vexe · Oct 06, 2013 at 12:46 PM 0
Share

Could you elaborate more? - What you mean you're converting it to score? - and yes, it counts per 1 sec, isn't that what you wanted?

avatar image Jamie_h88 · Oct 06, 2013 at 12:57 PM 0
Share

I have a score which is currentScore*timer, this is in an Int. this score is displayed on screen and is giving me strange results.

avatar image vexe · Oct 06, 2013 at 01:08 PM 0
Share

No problem there, just cast timer to an int - in C# it would be score = currentScore * (int)timer; however in JS, see this.

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

16 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

Related Questions

How to realize accurate time counter (millisecond precision) 3 Answers

Timer Continues After Object is disabled 2 Answers

How to subtract 1 from an int each second? 2 Answers

Time Freezer 1 Answer

How to calculate 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