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 Mr Bo · Oct 02, 2012 at 11:21 AM · guitimerresetcounter

reset timer using a GUI button

in my game you have to collect things in a certain amount of time. If you have failed you must re-do the level or if you pass the time will become less

I want to reset timer using a GUI button, should i be using delta instead of time.time as i know this counts from the start of the scene

when i click my GUI button the timer stays at 0 please help :)

this is my script so far

var isPaused : boolean = false;

static var startTime : float; //(in seconds)

static var round : boolean;

var timeRemaining : float; //(in seconds)

var GuiText : GUIText;

function Awake(){

startTime = 10.0;

}

function Update() {

if (!isPaused) { // make sure the timer is not paused DoCountdown(); } }

function timer(){

startTime = 10.0;

isPaused = false;

}

function DoCountdown() {

timeRemaining = startTime - Time.time;

ShowTime();

if (timeRemaining < 0) {

Roundendscript.End = true;

GameController.Over = true;

isPaused = true;

timeRemaining = 0;

TimeIsUp(); }

}

function PauseClock() { isPaused = true; }

function UnpauseClock() { isPaused = false; }

function ShowTime() { var minutes : int;

var seconds : int;

var timeStr : String;

minutes = timeRemaining/60;

seconds = timeRemaining % 60;

timeStr = minutes.ToString() + ":";

timeStr += seconds.ToString("D2");

GuiText.text = timeStr; //display the time to the GUI

}

function TimeIsUp() {

 Debug.Log("Time is up!");

}

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 Dreamblur · Oct 02, 2012 at 11:29 AM 0
Share

I don't even see an OnGUI function anywhere.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by mattbenic · Oct 02, 2012 at 12:21 PM

You should be setting your startTime to Time.time and then comparing that to Time.Time in your tests:

 var timeRemaining : float;
 var startTime : float;
 var timeLimit : float; // Set this to whatever you want your limit to be in seconds, so 10 in your case
 
 function Awake(){
     resetClock();
 }
 
 function resetClock(){
     startTime = Time.Time;
     isPaused = false;
 }
 
 function Update() {
     if (!isPaused) { 
         // make sure the timer is not paused 
         DoCountdown(); 
     } 
 }
 
 
 function DoCountdown() {
     var timeElapsed = Time.time - startTime;
     timeRemaining = timeLimit - timeElapsed;
     ShowTime();
 
     if (timeRemaining < 0) {
         Roundendscript.End = true;
         GameController.Over = true;
         isPaused = true;
         timeRemaining = 0;
         TimeIsUp(); 
     }
 }
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 Student Juice · Dec 13, 2012 at 05:30 PM

thanks for your answer but im still having problems:

ullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (IConvertible convertible) Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (System.Object value) Boo.Lang.Runtime.RuntimeServices.UnboxSingle (System.Object value) Timer.resetClock () (at Assets/Scripts/Timer.js:15) GameController.OnGUI () (at Assets/Scripts/GameController.js:17)

im calling the fuction from my gui

gameObject.Find("Timer").GetComponent(Timer).resetClock();

but it still doesnt work when removed

start and timeRem are set to 0 in viewport

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

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

13 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

Related Questions

GUI Help, timer and counter 1 Answer

C# countdown timer 9 Answers

how to create race lap timer, start to finish, display gui? 1 Answer

Counter doesn't reset 1 Answer

Start a timer after instantiating a game object 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