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 thetimmy997 · Jan 15, 2019 at 09:20 AM · timerstringcompare

Compare two time strings?

So I have a timer code that counts up when starting a game. Then I convert the output of the timer to a highscore variable(string) after the game is finished. So if I start a new game, I want to check whether the new output of the timer is greater or lower than the highscore variable? Any suggestions?

Timer code:

     public Text counterText;
 
     public float seconds, minutes, startTime;
 
     // Initialization
     void Start()
     {
         counterText = GetComponent<Text>() as Text;
         startTime = Time.time;
     }
 
     // Update once per frame
     void Update()
     {
         minutes = (Time.time - startTime) / 60f;
         seconds = (Time.time - startTime) %  60f;
         counterText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
     }

and the timer stops when it reaches the finish line(trigger)

     public GameObject successfulCanvas;
     public Text score;
     string finalScore = "None";
 
     private void OnTriggerEnter2D(Collider2D collision)
     {
         if (collision.CompareTag("Player"))
         {
             Time.timeScale = 0;
             successfulCanvas.SetActive(true);
 
             if(score.text >= finalScore) //help here
 
             finalScore = score.text; //ignore
         }
     }
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
0

Answer by Hellium · Jan 15, 2019 at 10:00 AM

 public Text counterText;
 
  private float seconds, minutes, startTime;

  public float TimeElapsed
  {
       return Time.time - startTime;
  }

 
  // Initialization
  void Start()
  {
      counterText = GetComponent<Text>() as Text;
      startTime = Time.time;
  }
 
  // Update once per frame
  void Update()
  {
      minutes = TimeElapsed / 60f;
      seconds = TimeElapsed %  60f;
      counterText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
  }


  public GameObject successfulCanvas;

  // Drag & drop the gameObject holding the `Time` script
  // (supposing your 1st script is called Timer)
  public Timer Timer;
  float highScore;
 
  private void OnTriggerEnter2D(Collider2D collision)
  {
      if (collision.CompareTag("Player"))
      {
          Time.timeScale = 0;
          successfulCanvas.SetActive(true);
 
          if(Timer.TimeElapsed >= highScore)
               highScore = Timer.TimeElapsed;
      }
  }
Comment
Add comment · Show 2 · 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 thetimmy997 · Jan 15, 2019 at 10:29 AM 0
Share

I cannot use a method to conduct mathematical operations with float. TimeElapsed is causing this.

avatar image Hellium thetimmy997 · Jan 16, 2019 at 07:27 AM 0
Share

I don't really understand what exact problem you are facing. I couldn't you use float comparison?

avatar image
0

Answer by xxmariofer · Jan 15, 2019 at 10:00 AM

The easiest solution is to use int.Parse (or Tryparse.) to convert you score string to int again

int _score = int.Parse(score.text);

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

102 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

Related Questions

Comparing two Strings. Returns equal and not equal at the same time? 3 Answers

how to Subtract string from TimeSpan 0 Answers

What's faster to compare, strings or typeof(s) ? 0 Answers

Check if specific object exist in list or array, Best Practices? 2 Answers

If Statement Acting Strange 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