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
1
Question by liamstevenhansen · Feb 02, 2019 at 02:56 AM · uitexturemathf

My timer won't work?

So I am making a game where when the timer hits one min you fail and have to restart. I am working with my friend on this he's pretty expeirenced and I am a beginner. This code is not working. using

System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class Timer : MonoBehaviour { Text text; float theTime; public float speed = 1f;

 Text MaxTime;
 Text CurrentTime;

 // Start is called before the first frame update
 void Start()
 {
     text = GetComponent<Text>();
     CurrentTime = GetComponent<Text>();
 }

 //Update is called once per frame
 void Update()
 {
    theTime += Time.deltaTime * speed;
    string hours = Mathf.Floor((theTime % 216000) / 3600).ToString("00");
    string minutes = Mathf.Floor((theTime % 3600) / 60).ToString("00");
    string seconds = (theTime % 60).ToString("00");
    text.text = hours + ":" + minutes + ":" + seconds;

     if (CurrentTime.text = "01:00:00".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 Collects · Feb 02, 2019 at 02:58 AM 0
Share

I can't help because I'm the friend you are talking about!

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by rfry336 · Feb 02, 2019 at 06:47 PM

I made a test game with a timer on the screen. The original code does work it was but the time was set to 1 hour.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 public class Timer : MonoBehaviour
 {
     [SerializeField] float speed = 1f;
 
     Text text;
     Text MaxTime;
     Text CurrentTime;
 
     float theTime;
 
     // Start is called before the first frame update
     void Start()
     {
         text = GetComponent<Text>();
         CurrentTime = GetComponent<Text>();
     }
     //Update is called once per frame
     void Update()
     {
         theTime += Time.deltaTime * speed;
         string hours = Mathf.Floor((theTime % 216000) / 3600).ToString("00");
         string minutes = Mathf.Floor((theTime % 3600) / 60).ToString("00");
         string seconds = (theTime % 60).ToString("00");
         text.text = hours + ":" + minutes + ":" + seconds;
         if (CurrentTime.text == "00:01:00".ToString())//This time was set to 1 hour. 
         {
             //Do something here when the time is up.
             Debug.Log("Time Is Up");
         }
 
     }
 
 
 }
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 zereda-games · Feb 19, 2019 at 09:46 AM 0
Share

By answer done by test above

So the main change is ?

  if (CurrentTime.text == "01:00:00")
  {
  }

to

  if (CurrentTime.text == "00:01:00")
  {
  }


Yes?

FYI:

  if (Equals(CurrentTime.text, "00:01:00"))
  {
  }

  1. Above is same as Top, but with this ! goes at front NOT in an != format. (CurrentTime.text != "00:01:00") to do a Not If equals statement.

  2. ToString isn't needed as it already is a string to begin with. Anything inside "" is a string...Not to mention it is missing () at the end of it. $$anonymous$$inda supersized that, that didn't make Unity whine at you!*

avatar image
1

Answer by andrew-lukasik · Feb 19, 2019 at 10:26 AM

I answered this question once before here: How to code timers without creating new strings every frame

NOTE: this solution is probably for more advanced programmers (at least because of that lambda/anonymous function syntax). Nevertheless you can still look up some useful ideas from there.

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 zereda-games · Feb 19, 2019 at 11:55 AM 1
Share

Doesn't seem That complicated, thanks for the Link. Someone should close this question though.

avatar image
0

Answer by toddisarockstar · Feb 02, 2019 at 05:02 AM

I am looking at your code and it hurts my eyes to see all that division. division is much harder on a CPU than multiplication and you are calling it all once per frame! anyways here is a simpler way that works

 public int min;
 public int sec;
 public int hour;
 public string show="00:00:00";
 public float f;

 void Update () {
     f += Time.deltaTime;
     if (f > 1) {f-=1;
         //one second has passed so now we do stuff
         sec++;if(sec==60){sec=0;min++;if(min==60){min=0;hour++;}}
         show = format (hour)+":"+format(min)+":"+format(sec);

         //check for time and update your display here
         print (show);
         }}
 public string format (int z){
     string r = "" + z;
     while(r.Length<2){r="0"+r;}
     return r;
 }
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 andrew-lukasik · Feb 19, 2019 at 10:22 AM 0
Share

1000 divisions per frame may be an issue. Few is probably just complete cpu-pennies when compared to object (string) allocations every frame

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

192 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 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 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to render html text in new UI system (unity 4.6) 0 Answers

All UI's appear as black 0 Answers

How can I "splat" a leaf texture randomly on my terrain? 0 Answers

Material on top of GUITexture 0 Answers

Unity 2018 UGUI Android Sprite Textures Not Rendering Correctly 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