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
1
Question by luniac · Feb 12, 2015 at 03:26 AM · iphonememory

iphone variable values reset on application focus!?

Hi guys the weirdest thing is happening in my app.

So i have 2 simple variables declared in a script:

 private var right : int;
 private var wrong : int;

i increment them as necessary during the GAME scene, and when i return to the MENU scene, i do this right before loading it:

 function LoadMenu(){
     PlayerPrefs.SetInt("right", right);
     PlayerPrefs.SetInt("wrong", wrong);
     Application.LoadLevel(1);
 }

In the menu i load those variables and do some calculation like so:

 var right : int = PlayerPrefs.GetInt("right",0);
 var wrong : int = PlayerPrefs.GetInt("wrong",0);
 var percent : float = 0;
 if((right + wrong) > 0){
     percent = ((100.0*right)/(right+wrong));
 }
 rightText.text = "RIGHT: " + right.ToString();
 wrongText.text = "WRONG: " + wrong.ToString();
 percentText.text = percent.ToString("0") + "%";



nice and simple right?

and on ANDROID IT WORKS PERFECTLY EVERY TIME.

But on iphone, if a user accrues some right and wrongs, then changes windows midgame to another application for some time, and then comes back to the game and returns to the menu, the variables right and wrong are ZERO AGAIN!!!

This only happens if the app is not come back to for over 10 minutes or so, if u quickly lave and come back to the app, then everything works fine and the variables are saved.

Is this an iPhone bug? some kind of weird memory leak issue of some sort on iOS???

i can fix this problem by saving the right and wrong variables to playerprefs as soon as theyre incremented, but i just wanna know what the actual problem is so i can look out for it in future apps.

Please any help is greatly appreciated!

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
1
Best Answer

Answer by Bunny83 · Feb 12, 2015 at 11:38 AM

You should make sure to save your values on OnApplicationFocus. iOS as well as Android can dump idle Apps out of memory at any time the system wants to. Since you only save your values before you switch to your load menu the values aren't saved until then. If the player pauses your app midgame it's always possible that the game has to restart completely.

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 luniac · Feb 12, 2015 at 11:49 AM 0
Share

thats creepy... so not a single variable declaration is safe? how come other things don't mess up? seems weird...

basically i should run PlayerPrefs.Save on all my variables every single OnApplicationFocus

avatar image MaxEden · Feb 12, 2015 at 12:00 PM 0
Share

Lol, I don't know about iOS, but on android you often don't even receive OnApplicationFocus event. Applications on mobile platforms are highly depend on serialized state and can be killed at any moment like by inco$$anonymous$$g call/popup. So make sure to save some critical states immediately, for example current level/game stats.

avatar image luniac · Feb 12, 2015 at 12:37 PM 0
Share

understood... sigh... life...

avatar image
1

Answer by MaxEden · Feb 12, 2015 at 11:17 AM

I believe, you forget to call PlayerPrefs.Save

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 luniac · Feb 12, 2015 at 11:48 AM 0
Share

o.O first time im seeing this! LOL thanks. id mark both of urs as answers if i could.

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

20 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

Related Questions

Large memory footprint increase when assigning a GUITexture from a WWW Object on iPad 1 Answer

How do I destroy things properly to free up memory? (iOS) 3 Answers

Clearing Editor References when Building for Mobile 1 Answer

Assets never unloading after loading next scene 1 Answer

lightmap vs normals 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