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 Deedah · Dec 07, 2015 at 09:02 PM · timerscore system

Saving and displaying best time per level.

Hello there, so I am working on a platformer/time-attack game that has the player trying to set and beat their best time. My timer works well, but I am trouble getting to save my best time for each level and getting it to display properly. If it's not too much to ask, could someone eyeball my code and give me a few pointers as to what I am doing wrong? Thanks in advance!

Here's my code:

 #pragma strict
 public static var startTime : float;
 public static var endTime: float;
 public var currentTimeLvel1: float = 0.0f;
 //public var currentTimeLvel2: float = 0.0f;
 //public var currentTimeLvel3: float = 0.0f;
 //public var currentTimeLvel4: float = 0.0f;
 //public var currentTimeLvel5: float = 0.0f;
 public var onLevel1 : boolean = true;
 //public var onLevel2 : bool = false;
 //public var onLevel3 : bool = false;
 //public var onLevel4 : bool = false;
 //public var onLevel5 : bool = false;
 
 private var bestTime : float = 0.0f ;
 public var level;
 var playTime = endTime - startTime;
 public var textTime : String;
 
 
 public var guiSkin : GUISkin;
 
 
 
 function Start() 
 {
    startTime = Time.time;
    bestTime = PlayerPrefs.GetFloat("Best Time:", Mathf.Infinity) ;
     Debug.Log ("Best Time:" + bestTime);
 }
          function Update()
      {
          if(onLevel1)
          {
              SetBestTime() ;
          }
      
         
      }
      
           function SetBestTime()
      {
          currentTimeLvel1 = Time.time;
          Debug.Log ("End Time:" + currentTimeLvel1);
          
                     if(currentTimeLvel1 < bestTime)
            {
               PlayerPrefs.SetFloat("Best Time:", endTime);
               bestTime = endTime;
            }
      }
      
 
      
     function OnGUI () 
         {
             
             var guiTime = Time.time - startTime; 
 
             var minutes : int = guiTime / 60;
             var seconds : int = guiTime % 60;
             var fraction : int = (guiTime * 100) % 100;
              
              textTime = String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction); 
 
              GUI.skin = guiSkin;
              GUI.Label(Rect((Screen.width / 2) - 100, 30, 350, 80), "TIME ELAPSED: " + textTime);
              GUI.Label(Rect((Screen.width / 2) - 100, 60, 350, 80), "BEST TIME: " + bestTime);
          
         }
         
             function LevelEnded()
         {
           
            PlayerPrefs.SetFloat("Best Time:", bestTime); 
            Debug.Log ("Best Time:" + bestTime);
            PlayerPrefs.Save();
           
         }
 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by MechanicalGaming · Dec 07, 2015 at 11:30 PM

Hey there!

Try creating 2 floats. First one is for the current time of starting the scene (the time follows itself through every scene) and then make a static float. at the end of the scene (or completion) stop the static float from being equal to the time and then deduct the starting time.

We use a static float for it so that it is not forgotten and so you can reference it in other scripts. make a save script and choose to save it in that. You can then decide to round to round it up or down and change it to minutes and everything like that. Hope this helps!

Got questions? Ask away!

Here's a sample to make it easier to understand:

 //Variables
 public float currentTime;
 public static float endTime;
 
 //Check if complete (must put something in to confirm it)
 public bool done = false;
 
 //Start of the scene
 void Start() {
   currentTime = Time.time
 }
 
 void Update () {
   if(done == false) {
       //Update the timer
       endTime = Time.time;
   } else if(done == true) {
       //Calculate the amount of time it took to complete the objective (Saved Variable)
       endTime = endTime - currentTime;
   }
 }
Comment
Add comment · Show 3 · 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 Deedah · Dec 08, 2015 at 12:00 AM 0
Share

Y'know, I was worried I wouldn't get an answer to this question of $$anonymous$$e for a few days if not ever, so first off, THAN$$anonymous$$ YOU SO $$anonymous$$UCH for your answer! Anywho, I'll rework my code a bit but I also need it to save to PlayerPrefs or an .xml to save that player's overall best time on that level so they can reopen the game and see that their previous best score for their levels are there if they want to try and beat it.

There's also the matter of getting all these times to the result screen with a GUI, any ideas on that (using PlayerPrefs or X$$anonymous$$L)?

avatar image MechanicalGaming Deedah · Dec 08, 2015 at 01:50 PM 0
Share

Your welcome! And if the game you are making is a web application then I don't think there is a need to save the game. In all honesty, there is a way but I won't be able tot help you out in that matter.

But if the game you are making is a android/apple game or computer game then you can save the details easily without using any X$$anonymous$$L. Here's a video that explains how to do it : https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading The video is very long but it's worth watching!

To use stuff in the GUI you just need to reference the static variable endTime in the text. Then if you really want you could do 2 buttons. One where they can beat their score (the game ends if it goes over and it restarts for example) or they just play it casually.

Also, I've just reread your code and it seems as if you are trying to put everything into the same script. $$anonymous$$y advice would be to create a script for the text, saving, level, ect... separately. You'll get lost a little less and also it'll make the game run a little better because the script reads itself line by line so the stuff that is at the end of the script will have a short delay before being read.

avatar image MechanicalGaming Deedah · Dec 09, 2015 at 12:42 AM 0
Share

If all of this works don't forget to say that make this thread an answered thread.

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

35 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

Related Questions

How to add one score every second to scoremanager c# 1 Answer

Looking for a simple Score Multiplier. 1 Answer

How do I add my Timer to my Score system in C# ? 0 Answers

Hey i am trying to add a time score system and time high score system need help. 0 Answers

Scoring based on count up timer 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