Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Taorcb · Jun 08, 2015 at 12:30 AM · timernulltimer-script

Checking the value of null

Now, I know the title seems stupid, but hear (read, whatever) me out. I have a script that times how long it takes for the player to complete a level and saves it as a playerpref. However, to check whether or not the time of the last game was better than the best time, the script looks at the playerpref for the best time. The problem is, when you start the game, you have no high score, and checking the playerpref returns null, which then cannot be used to check whether or not the last game's time was faster than the best time. Here's the script (C#):

 //Times games and saves the best times to their respective PlayerPrefs
 
 using UnityEngine;
 using System.Collections;
 
 public class Timer : MonoBehaviour {
 
     public static float Seconds = 0;     //Seconds passed within this minute
     public static int Minutes = 0;       //Minutes passed
     public static string RealTime;       //The time in the form of X:YZ
     public static float TimeCompare;     //The value used to determine if this game's time is better than the previous
 
     public static float LastTime1 = 0;   //Fastest time values are zero by default
     public static float LastTime2 = 0;
     public static float LastTime3 = 0;
     public static float LastTime4 = 0;
 
     void Start()                        //Get the values for the best time of each level
     {
         LastTime1 = PlayerPrefs.GetFloat("LastTime1");
         LastTime2 = PlayerPrefs.GetFloat("LastTime2");
         LastTime3 = PlayerPrefs.GetFloat("LastTime3");
         LastTime4 = PlayerPrefs.GetFloat("LastTime4");
 
         Seconds = 0f;           //Reset Seconds
         Minutes = 0;            //Reset Minutes
         RealTime = null;        //Reset RealTime
         TimeCompare = 0;        //Reset TimeCompare
     }
     // Update is called once per frame
     void Update () {
 
         Seconds = Time.timeSinceLevelLoad - Minutes*60;
 
         if (Seconds >= 60) 
         {
             Minutes += 1;               //If 60 seconds have passed, add one to minutes
         }
 
         TimeCompare = Time.timeSinceLevelLoad;
 
         RealTime = (Minutes.ToString() + ":" + Seconds.ToString("F0")); //Convert the Minute and Second variables into a readable value
         Debug.Log(RealTime);
 
         if (LoadLevel.GameOver == true)
         {
 
             if (LoadLevel.Level == 1 && TimeCompare > LastTime1)
             {
                 PlayerPrefs.SetFloat("LastTime1", TimeCompare);
                 PlayerPrefs.SetString("Level1Time", RealTime);
                 PlayerPrefs.Save();
             }
 
             else if (LoadLevel.Level == 2 && TimeCompare > LastTime2)
             {
                 PlayerPrefs.SetFloat("LastTime2", TimeCompare);
                 PlayerPrefs.SetString("Level2Time", RealTime);
                 PlayerPrefs.Save();
             }
 
             else if (LoadLevel.Level == 3 && TimeCompare > LastTime3)
             {
                 PlayerPrefs.SetFloat("LastTime3", TimeCompare);
                 PlayerPrefs.SetString("Level3Time", RealTime);
                 PlayerPrefs.Save();
             }
 
             else if (LoadLevel.Level == 4 && TimeCompare > LastTime4)
             {
                 PlayerPrefs.SetFloat("LastTime4", TimeCompare);
                 PlayerPrefs.SetString("Level4Time", RealTime);
                 PlayerPrefs.Save();
             }
         }
     }
 }
 
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 Dave-Carlile · Jun 08, 2015 at 12:31 AM 0
Share

What is giving you null? GetFloat can't return null because it's returning a float which isn't nullable.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Ricewind1 · Jun 08, 2015 at 12:44 AM

Add a check that sees if Playerprefs is null or not. Just skip setting playerprefs or return null. Load and set playerprefs related fields in void Awake() or make playerprefs static and load playerprefs in its getter.

Example below:

 public class ItemDatabase
 {
     public int DatabaseID = 0;
     public Dictionary<String, Item> ItemList;
 
     public ItemDatabase(int databaseID)
     {
         this.DatabaseID = databaseID;
         ItemList = new Dictionary<String, Item>();
     }
 
     static ItemDatabase iDatabase;
 
     public static ItemDatabase activeDatabase
     {
         get
         {
             if (iDatabase == null)
             {
               iDatabase = //Load database
             }
             return iDatabase;
         }
     }
 
     public static Item FindByID(int itemID)
     {
         ItemDatabase db = activeDatabase;
         db.GetItemByID(itemID);
     }
 
     private Item GetItemByID(int itemID)
     {
         return item;
     }

Replace Itemdatabase with playerprefs. This will allow you to always call playerprefs from anywhere without a reference and handle the loading in playerprefs itself.

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

Answer by Taorcb · Jun 08, 2015 at 05:29 AM

Fixed it - the player press weren't saving because the script required TimeCompare to be greater than LastTime, which was set to zero in the declaration field (is that what that's called?).

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to I get the timer counter to restart when the player hits the ground and respawns? 2 Answers

How do I implement a timer into this script that will record the total time the player has played. 2 Answers

Timer Text doesn't show the time in the script. 1 Answer

Not sure what is wrong with my timer script. 1 Answer

Reload level after timer hits 0 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