Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 heaversm · Jul 28, 2011 at 03:23 PM · timermenuongui

gameTimer needs to be based on an event, not onGUI

Hi - I have a timer function set up which I programmed based on some posting here, but it is set up to run through the onGUI event, so the time is based on the amount of time the game has been running (I guess). But what I want it to do is not start the timer til the user clicks the logo to begin play. The way I have it set up now, if the user takes 10 seconds to click the start button, and the game timer is set to count down from 60 seconds, by the time they begin play there will only be 50 seconds left in the game. How do I change this to trigger after the click?

Here's my code:

TIMER SCRIPT:

 private var startTime = 60;
 var textTime : String; //added this member variable here so we can access it through other scripts
 var beginPlay : boolean = false;
 var guiTime : int;
 
 function OnGUI () {
     if (beginPlay == true){
         guiTime = startTime - Time.time; //how to count down
         var timeText = guiTime.ToString();
         if (guiTime < 0){
             //END GAME
         } else {
             timerTextRef.text = timeText;
             textTime = timeText;
         }
     }
 }

MENU SCRIPT:

 var cameraRef : GameObject;
 
 function Update () {
       if (Input.touchCount > 0 ) {
         for(var i : int = 0; i< Input.touchCount;i++) {
               var touch : Touch = Input.GetTouch(i);
               if  ( touch.phase == TouchPhase.Began && guiTexture.HitTest(touch.position)) {
                 guiTexture.enabled = false;
                 cameraRef.GetComponent(Timer).beginPlay = true;
               }
         } 
     }
 }
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

2 Replies

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

Answer by heaversm · Jul 28, 2011 at 06:32 PM

I ended up doing this. I'm sure someone will inform me that this is the wrong way to do timers, but it worked for my purposes:

 function Start(){
     startTime = 10;
     timeDown();
 }
 
 function timeDown(){
     yield WaitForSeconds (1);
     startTime--;
     repeatTime();
 }
 
 function repeatTime(){
     if (startTime < 1){
         endGame();
     } else {
         timeDown();
     }
 }
Comment
Add comment · 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
0

Answer by DaveA · Jul 28, 2011 at 05:03 PM

You need to set startTime when the touch is detected, not on start (by assigning it statically), so put startTime = 60 after, say , guiTexture.enabled = false;

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 heaversm · Jul 28, 2011 at 05:18 PM 0
Share

If I do that, the countdown (Time.time in onGUI) is still being based off of the total game time, and I also get an error in onGUI that startTime does not refer to anything because it hasn't been instantiated yet. What I need to figure out is how to make this not based on onGUI, right?

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

if statement will only execute once 1 Answer

"Escape" not working to open menu? 1 Answer

Best way to display a (highscore) dialogue 0 Answers

Shift from one scene to another (based on time) 1 Answer

Alternate solution to onGUI for GUI? 4 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