• 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 PvTGreg · Sep 12, 2014 at 01:47 PM ·

Sorting Best time

Ok ive been looking at my code for so long that i cant see whats wrong with it so could i have some help sorting the time atm it dosent sort it properly

edit: what i am doing is timing the player to complete a level the time is taken and sorted then the player can start again it resets time and start counting again and so on

 using UnityEngine;
 using System.Collections;
 using System.IO;
 using System;
 public class Score : MonoBehaviour {
     
     public int Time;
     public int BestTime;
     public int SecondBestTime;
     public int T$$anonymous$$rdBestTime;
     
     
     void Start () 
     {    
         Time = PlayerPrefs.GetInt("Time");
         BestTime = PlayerPrefs.GetInt("Best Time");
         SecondBestTime = PlayerPrefs.GetInt ("Second Best Time");
         T$$anonymous$$rdBestTime = PlayerPrefs.GetInt ("T$$anonymous$$rd Best Time");
         InvokeRepeating("Points", 2, 0.3F);
     }
     
     
     // Update is called once per frame
     void Points () 
     {
         Time += 1;
         PlayerPrefs.SetInt("Time",Time);
 
         if (Time >= BestTime || Time < BestTime)
         {
             // Set the $$anonymous$$ghscore to our current score
             BestTime = Time;
             // savign $$anonymous$$ghscore
             PlayerPrefs.SetInt("Best Time", BestTime);
         }
         else 
         {
             if(Time >= SecondBestTime && Time > BestTime && Time > T$$anonymous$$rdBestTime)
             {
                 SecondBestTime = Time;
                 PlayerPrefs.SetInt("Second Best Time", SecondBestTime);
             }
         }
         
         if (Time >= T$$anonymous$$rdBestTime && Time < SecondBestTime && Time < BestTime )
         {
             // Set the $$anonymous$$ghscore to our current score
             T$$anonymous$$rdBestTime = Time;
             // savign $$anonymous$$ghscore
             PlayerPrefs.SetInt("T$$anonymous$$rd Best Time", T$$anonymous$$rdBestTime);
         }
         
         
         
     }
 
     void OnGUI()
     {
         Time = PlayerPrefs.GetInt("Time");
         GUI.Box(new Rect(200,10,100,30),"Time:"+Time.ToString());
 
         
             
     
 
             
             
         
     }
 }
 
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
3
Best Answer

Answer by dsada · Sep 12, 2014 at 01:54 PM

i dont really understand what you are trying to do but you could just make sometihng like t$$anonymous$$s:

  1. add your points in a 'List';

  2. call list.Sort();

  3. you are done! list[0] is the best time list[1] is the second best time, etc....

Comment
Add comment · Show 7 · 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 PvTGreg · Sep 12, 2014 at 02:01 PM 0
Share
avatar image PvTGreg · Sep 12, 2014 at 02:06 PM 0
Share
avatar image PvTGreg · Sep 12, 2014 at 02:12 PM 0
Share
avatar image HarshadK · Sep 12, 2014 at 02:12 PM 0
Share
avatar image dsada · Sep 15, 2014 at 08:30 AM 1
Share
Show more comments
avatar image
1

Answer by HarshadK · Sep 12, 2014 at 01:55 PM

Try t$$anonymous$$s:

 if (Time >= BestTime)
 {
     // Set the $$anonymous$$ghscore to our current score
     BestTime = Time;
     // savign $$anonymous$$ghscore
     PlayerPrefs.SetInt("Best Time", BestTime);
 }
 else
 {    
     if(Time >= SecondBestTime && Time < BestTime)
     {
         SecondBestTime = Time;
         PlayerPrefs.SetInt("Second Best Time", SecondBestTime);
     }
     else if (Time >= T$$anonymous$$rdBestTime && Time < SecondBestTime)
     {
         // Set the $$anonymous$$ghscore to our current score
         T$$anonymous$$rdBestTime = Time;
         // savign $$anonymous$$ghscore
         PlayerPrefs.SetInt("T$$anonymous$$rd Best Time", T$$anonymous$$rdBestTime);
     }

}

Comment
Add comment · Show 5 · 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 PvTGreg · Sep 12, 2014 at 02:04 PM 0
Share
avatar image PvTGreg · Sep 12, 2014 at 02:06 PM 0
Share
avatar image gjf · Sep 12, 2014 at 02:07 PM 0
Share
avatar image HarshadK · Sep 12, 2014 at 02:11 PM 1
Share
avatar image PvTGreg · Sep 12, 2014 at 02:13 PM 0
Share

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

24 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

Related Questions

Create Array Accessible By Any Script 2 Answers

Add force based on swipe speed, direction, curve 3 Answers

helpme please to make infinite map??? 0 Answers

I am having problem with lights in unity iOS. 2 Answers

Help with fullscreen jumpscare 2 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