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 moghes · Oct 14, 2013 at 08:53 AM · stringformatbinarylong

Can't Convert string binary to long.

Hello everyone,

I have a problem, it might be a trivial problem though..

I need to save the last date and time when the user leaves the game or quits the game. And get back when starts the game again, just to calculate the difference in time, since there are factors that will be decremented with respect to time.

 void Start () 
 {
     CalculateTimeDifference(); 
 }
    
 void OnApplicationPause(bool pauseStatus)
 {
   paused = pauseStatus; // Check the state of the application
                 
    if(paused)
       PlayerPrefs.SetString("sDate",System.DateTime.Now.ToBinary().ToString());                                 
             
    if(!paused)                    
         CalculateTimeDifference();        
 }
 
 
 void OnApplicationQuit()
 {
     PlayerPrefs.SetString("sDate",System.DateTime.Now.ToBinary().ToString());
 }
 
 
 private void CalculateTimeDifference()
 {
     currentDate = System.DateTime.Now;
     long temp = Convert.ToInt64(PlayerPrefs.GetString("sysString"));
     DateTime oldDate = DateTime.FromBinary(temp);
     TimeSpan difference = currentDate.Subtract(oldDate);
 }


When CalculateTimeDifference() is called in the start function, it gives me ar error. The errors says:

"FormatException: Input string was not in the correct format System.Int64.Parse (System.String s)"

I can't guess what might be wrong with this ?? any help please

Comment
Add comment · Show 4
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 mattssonon · Oct 14, 2013 at 09:13 AM 1
Share

Are you sure PlayerPrefs.GetString("sysString") returns a string? Is it set?

avatar image fafase · Oct 14, 2013 at 09:21 AM 1
Share

Try to decompose your code:

 string __temp = PlayerPrefs.GetString("sysString");
 print(__temp);
 long temp = Convert.ToInt64(__temp);

Then that will tell you how the string looks. I think those Convert methods do not like if you have anything else but numbers, so if for some reasons an extra character is there, that would clash. For instance a float.

FormatException : value does not consist of an optional sign followed by a sequence of digits (0 through 9).

avatar image moghes · Oct 14, 2013 at 09:14 PM 0
Share

It appeared my mistake!

It seems that OnApplicationPause is being called not only after pausing and returning the game, but every time the game starts (I am testing in the editor), I moved my function call in Start and it got solved.

avatar image mattssonon · Oct 15, 2013 at 07:15 AM 0
Share

All right, please post an answer and accept it, so the question gets closed. :)

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by moghes · Oct 15, 2013 at 11:43 AM

OnApplicationPause was called everytime I start the game, and it is called before the Start function.

I added

 if(!paused)        
 {
    if(PlayerPrefs.HasKey("sysString"))
    {
     CalculateTimeDifference();    
    }
 }

CalculateTimeDifference() function Gets from PlayerPrefs, while the first time or after (PlayerPrefs.DeleteAll), so there was no value (null) for PlayerPrefs.GetString("sysString"), as mattssonon helped. Adding PlayerPrefs.HasKey("sysString") will not call the function if no key, plus I removed the function call from the Start function.

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
1

Answer by ArkaneX · Oct 14, 2013 at 09:26 AM

You're writing sDate but reading sysString.

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 moghes · Oct 14, 2013 at 09:12 PM 0
Share

thanks dude for your reply, but that's been proper in my code.. I've been pasting portions of my code just to avoid huge posts and thats by mistake

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

15 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

Related Questions

String format to show float as time 3 Answers

Export objects to a .3DS file at runtime 1 Answer

How to stop WWW from changing request url? 1 Answer

what package?namespace? do I need to make use of String.Format() for C# programs? 4 Answers

FormatException: Input string was not in the correct format 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