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 dalekandjedi · Jul 23, 2012 at 05:31 PM · timer

Having problems with my timer

Hey i need help i have made a timer and it works but i want it to activate when its in a level not from the very beginning of the game cos when its a 3 mins timer but when i built the game and went though to the level though my menu it was 2:30mins cos it was playing though the time i was on the menu please help

here is my script

 var startTime : float; 
 var timeRemaining : float; 
 
 function Start()
 { 
     startTime = 180.0;
     guiText.material.color = Color.red; 
 }
 
 function Update () { 
     Countdown(); 
 }
 
 function Countdown(){ 
     timeRemaining = startTime - Time.time;
     if(timeRemaining > 0){ 
         ShowTime(); 
     }
     ShowTime(); 
     if(timeRemaining < 0) {
         timeRemaining = 0;
         TimeIsUp();
         Debug.Log("Time remaining = " + timeRemaining); 
     } 
 } 
 
 function ShowTime(){ 
     var minutes: int; 
     var seconds: int; 
     var timeString : String;
     minutes = timeRemaining / 60; 
     seconds = timeRemaining % 60; 
     timeString = minutes.ToString() + ":" + seconds.ToString("D2");
     guiText.text = timeString; 
 }
 
 function TimeIsUp(){
     Debug.Log("Time Is Up"); 
 }

Thanks

Comment
Add comment · Show 16
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 Bovine · Jul 23, 2012 at 05:34 PM 0
Share

Please refactor your script code - it is unreadable.

You could leave the GameObject inactive until the game starts and then activate it. If your time is in a different scene you could reset it when the scene loads - OnLevelWasLoaded.

avatar image AlucardJay · Jul 23, 2012 at 05:41 PM 1
Share

I just formatted your code, please do this in future.

As Bovine said, disable the call to Countdown() in the update until the menu has been cleared e.g. create a boolean, set it to false. When the menu has been closed, set the boolean to true. In update, check if boolean is true, then Countdown();

avatar image dalekandjedi · Jul 23, 2012 at 06:00 PM 0
Share

But I dont know how to make a boolean please help i really cant do it.

avatar image Bovine · Jul 23, 2012 at 06:03 PM 0
Share

This post will help then:

http://answers.unity3d.com/questions/26975/how-to-create-boolean-varibles-.html

You might note that I didn't know either (being a C# bod) but I took 30 seconds to surf the web for an answer - you might want to try doing that yourself.

avatar image AlucardJay · Jul 23, 2012 at 06:26 PM 1
Share

here's a little demo of a boolean in action. Attach this script to the camera in a new empty scene. Click the left mouse button , then the right, etc. You can see how a boolean works.

 #pragma strict

 var myBool : boolean = false;

 function Update() 
 {
     if ( Input.Get$$anonymous$$ouseButtonDown(0) )
     {
         myBool = true;
     }
     if ( Input.Get$$anonymous$$ouseButtonDown(1) )
     {
         myBool = false;
     }
 }

 function OnGUI() 
 {
     if ( myBool == true ) // this is usually written as : if ( myBool )
     {
         GUI.Box(Rect(10, 10, 200, 25), "myBool is TRUE");
     }
     else if ( myBool == false ) // this is usually written as : if ( !myBool ) : note the !
     {
         GUI.Box(Rect(10, 10, 200, 25), "myBool is FALSE");
     }
 }

Using this and all the help in these comments, you should be able to work out how to use a boolean in your update before Countdown();

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by james_170482 · Jul 23, 2012 at 06:47 PM

could'nt you add the script to an empty gameobject in the scene, so it will only start when the scene is loaded?

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 AlucardJay · Jul 23, 2012 at 06:51 PM 0
Share

No, the problem is the scene is starting, but the countdown is NOT to start until the menu has been viewed and dismissed.

avatar image Bovine · Jul 23, 2012 at 06:53 PM 0
Share

But you could do this, so you return to the meny by loading a different scene and start the game by loading a level of your game. But you have some fundamentals to understand I think.

avatar image dalekandjedi · Jul 23, 2012 at 06:55 PM 0
Share

Thanks guys ^_^ it works thanks for all your help sorry if ive been a bother ^^

avatar image AlucardJay · Jul 23, 2012 at 07:01 PM 0
Share

true and fair enough, I did not consider transitioning from menu to new scene to start timer.

(look at all these comments!)

@dalekandjedi what we were all pushing for is do you understand the idea of a boolean and how to use it. How about you submit an answer with your results, then you can accept it. If it's right, I'll give you a thumb Up for effort =]

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

8 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

qrest system 1 Answer

Help with movement script 2 Answers

Need Help Understanding Script! (Javascript) 1 Answer

i need some code suggestions 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