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 valax · Dec 28, 2011 at 09:37 PM · javascriptcountdown

Countdown not counting down

Hi

I'm relatively new to scripting and I made this script so that when 4 players (1 at the moment for testing) enter an area a countdown starts. The problem is that it isn't counting down. Can someone please help me by making it count! Also when it gets to 0 how do I get the player to quit the scene? Thanks.

 var playersAtEndPoint = 0;
 
 var guiskin : GUISkin;
 
 var timeTotal = 1800;
 
 var timeUntilEnd = timeTotal / 60;
 
 
 
 function OnTriggerEnter (Player : Collider){
 
     playersAtEndPoint ++;
 
 }    
 
     
 
 function OnTriggerExit (Player : Collider){
 
     playersAtEndPoint --;
 
 }
 
     
 
 function Update (){
 
 
 
     if (playersAtEndPoint == 1){
 
         timeTotal = timeTotal - Time.deltaTime;    
 
     }
 
     
 
     if (playersAtEndPoint == 0){
 
         timeTotal = 1800;
 
     }
 
     
 
 }
 
 
 
 function OnGUI(){
 
 
 
     GUI.backgroundColor = Color.black;
 
     GUI.contentColor = Color.white;
 
     
 
     GUI.skin = guiskin;
 
       GUI.Label(Rect(1,1,100,100),playersAtEndPoint.ToString());
 
       GUI.Label(Rect(8,8,100,100),"/4");
 
       
 
       if (playersAtEndPoint == 1){
 
           GUI.Label(Rect(1,30,100,100),"Time left:" + timeUntilEnd.ToString());
 
     }
 
 }
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 spacepilot · Dec 28, 2011 at 11:20 PM 0
Share

It's probably just a thing of style, but for

 if (playersAtEndPoint == 1){

boolean-variables are common. This way they get declared:

 var playersAtEndPoint : boolean = false;



The if-statement is written this way then:

 if (playersAtEndPoint){
 }

A negative-condition would look like this:

 if (!playersAtEndPoint){
 }

which is equivalent to:

 if (playersAtEndPoint == false){
 }

To set the variable's state in the script:

 playersAtEndPoint = true;

and

 playersAtEndPoint = false;

are used. If you just want to switch the state:

 playersAtEndPoint = !playersAtEndPoint;

which will always turn the variable into it's opposite state.

1 Reply

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

Answer by spacepilot · Dec 28, 2011 at 11:06 PM

Copy the calculation

 var timeUntilEnd = timeTotal / 60;

to the end of your script, so that the last lines read:

 if (playersAtEndPoint == 1){
   timeUntilEnd = timeTotal / 60;
   GUI.Label(Rect(1,30,100,100),"Time left:" + timeUntilEnd.ToString());
 }

Doing the math outside the function makes it getting calculated only once, before any function is processed.

Comment
Add comment · Show 1 · 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 valax · Dec 29, 2011 at 12:38 PM 0
Share

It's working now, thanks.

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

Cube terrain with perlin noise 1 Answer

Click To Revert to Original Texture else Destroy script help 1 Answer

What is wrong with this script? 2 Answers

How to Implement System.StringBuilder into JavaScript? 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