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 /
  • Help Room /
avatar image
0
Question by TamHartman · Sep 15, 2015 at 04:49 PM · guitimerguitexttimer countdowntimer-script

Problems showing accurate time with FixedUpdate()

Hey Guys, I'm working on this script that handles a countdown (3 seconds) until game start, the countdown for the duration of the game (30 seconds), and the game over sequence. So far it's working ok, but when I run it the time looks really janky. It's especially having problems switching from the pre-game countdown to the actual game countdown. Anyway, I'm wondering if you guys can help me get it working a little bit better, displaying the time more accurately.

 public var GUIFontStyle : GUIStyle = new GUIStyle() ;
 public var startObject : GameObject;
 public var gameOver : GameObject;
 public var scoreKeeper : scoreCollector;
 public var starttime : float;
 public var gametime : float = 30;
 public var rounds : int = 1;
 public var XPos = 200;
 public var YPos : int = 0;
 public var tWidth : int = 200;
 public var tHeight : int = 20;
 public var countdown : int = 3;
 public var message : String = "Start";
 function Start (){
     starttime = Time.time;
 
 }
 
 function FixedUpdate(){
     /*if (Time.time > gametime + starttime){
         starttime = Time.time;
     }*/
 }
 
 function OnGUI(){
     
     var countDownTime : float = (starttime + countdown) - Time.time;
     var timeleft : float = (starttime + gametime + countdown+1) - Time.time;
 
     var display : float;
 
     if (countDownTime > 1) {
         display = countDownTime;
         GUI.Label (Rect(XPos, YPos, tWidth, tHeight), display.ToString("0"),GUIFontStyle);
     }
     if (countDownTime < 1 && countDownTime > -1){
         startObject.SetActive(true);
         display = 30;
         GUI.Label (Rect(XPos, YPos, tWidth, tHeight), display.ToString("0"),GUIFontStyle);
     }
 
     else if (timeleft <= 30){
         if (timeleft >= 0){
         startObject.SetActive(false);
         display = timeleft;
         GUI.Label (Rect(XPos, YPos, tWidth, tHeight), display.ToString("0"),GUIFontStyle);
         }
         if (timeleft < 0){
             gameOver.SetActive(true);
         }
     }
     //GUI.Label (Rect(XPos, YPos, tWidth, tHeight), display.ToString("0"),GUIFontStyle);
 }
 
 function newGame(){
     gameOver.SetActive(false);
     starttime = Time.time;
     scoreKeeper.score = 0;
 
 }
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 Positive7 · Sep 15, 2015 at 05:33 PM 0
Share

How about ?

  function Update(){
      if (Time.time > gametime + starttime){
          starttime = Time.time;
      }
  }

FixedUpdate() should be used for physics (Rigidbodies, AddForce etc....)

http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.FixedUpdate.html

or if you really want to use FixedUpdate() consider using deltaTime

 function FixedUpdate(){
      if (Time.time > gametime + starttime){
          starttime = Time.deltaTime;
      }
  }

or using a Corutine :

 #pragma strict
 
  public var GUIFontStyle : GUIStyle = new GUIStyle() ;
  public var startObject : GameObject;
  public var gameOver : GameObject;
  public var score$$anonymous$$eeper : scoreCollector;
  public var starttime : float;
  public var gametime : float = 30;
  public var rounds : int = 1;
  public var XPos = 200;
  public var YPos : int = 0;
  public var tWidth : int = 200;
  public var tHeight : int = 20;
  public var countdown : int = 3;
  public var message : String = "Start";
  
  var firstCounter : float;
  var secondCounter : float;
  
  function Start (){
      StartCoroutine(Timer(countdown, gametime));
  }
  
  function Timer(first : float, second : float){
   while (first > 0) {
          first -= Time.deltaTime;
          GUI.Label (Rect(XPos, YPos, tWidth, tHeight), first.ToString("0"),GUIFontStyle);
          firstCounter = first;
          yield null;
      }
      Debug.Log("First Counter finished!");
      
      yield WaitForSeconds(0.1);
       
      while (second > 0) {
          second -= Time.deltaTime;
          GUI.Label (Rect(XPos, YPos, tWidth, tHeight), second.ToString("0"),GUIFontStyle);
          secondCounter = second;
          yield null;
      }
        Debug.Log("Second Counter finished!");
  }
  
  
  function OnGUI(){
       if(firstCounter > 0.1){
      GUI.Label (Rect(XPos, YPos, tWidth, tHeight), firstCounter.ToString("0"),GUIFontStyle);
      }
      if(secondCounter > 0){
      GUI.Label (Rect(XPos, YPos, tWidth, tHeight), secondCounter.ToString("0"),GUIFontStyle);
      }
  }
  
  function newGame(){
      gameOver.SetActive(false);
      starttime = Time.time;
      score$$anonymous$$eeper.score = 0;
  
  }

0 Replies

· Add your reply
  • Sort: 

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

Help with a timer 1 Answer

My countdown timer doesn't work,I'm trying to create a countdown timer 0 Answers

Creating a timer that starts when my 3rd scene is loaded 1 Answer

Hi there. i´m new to unity and i´m building a labyrinth. my question is how to add a timer countdown? 1 Answer

How to get left time from countdown to point? 0 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