• 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 TauseefCVS · Sep 26, 2016 at 04:59 PM · c#playerprefsloadsavingonapplicationquit

onapplicationquit() function is not working with unity 4.6.3 (android)

Hi Guys

i am using unity 4.6.3 , on that version onapplicationquit() function is not working , i want to save playerprefs in that function but it is not save the values and game state and also use dontdestroyonload() function


using UnityEngine;

using System.Collections;

using System;

public class scriptss : MonoBehaviour {

 private void OnApplicationQuit ()
 {
     PlayerPrefs.SetInt ("chart", 0);
     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 Zendist · Sep 26, 2016 at 08:11 PM 0
Share

$$anonymous$$ake void OnApplicationQuit() non-private.

3 Replies

· Add your reply
  • Sort: 
avatar image
-2

Answer by Zendist · Sep 26, 2016 at 08:25 PM

OnApplicationQuit must not be private, in order for Unity to call the function when it closes the application. Simply remove the private keyword in front of void OnApplicationQuit().

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 Hanoble · Sep 26, 2016 at 08:57 PM 0
Share

I do not believe this answer is correct. Private is implicit in C#, meaning that if you simply put no private or public access modifier, it is treated as private. Removing the "private" from the method does absolutely nothing different than having the private there. The reason why a programmer would explicitly declare private is for better cross-language compatibility, and for other programmers to have no doubt about the structure of the class.

Just to clarify, these are the same:

 void OnApplicationQuit()
 {
      Debug.Log("Application quit");
 }

 private void OnApplicationQuit()
 {
      Debug.Log("Application quit");
 }

I also tested this in the editor, just to be sure there was nothing quirky here, and the private modifier changes nothing.

avatar image
0

Answer by TauseefCVS · Sep 27, 2016 at 06:24 AM

#imageHanoble there no difference in private and without private is work as same

OnApplicationQuit() working on Unity editor but not working on android build thats my question

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
2

Answer by Hanoble · Sep 27, 2016 at 09:08 AM

OnApplicationQuit() is NOT guaranteed to be called on Android. You can read more about that here, but Android activities that have been paused are NOT guaranteed to always give a Quit callback. Due to this design, many developers instead save crucial data at other points (scene loads, game over screens, checkpoint, etc). If you need this precise data on any application suspension (with a quit possibly ahead), a popular alternative is OnApplicationPause().

Instead of what you have, try this:

 private void OnApplicationPause()
  {
      PlayerPrefs.SetInt ("chart", 0);
      PlayerPrefs.Save ();
  }

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Save/Load Animation State of Instantiated Prefabs 0 Answers

Multiple Cars not working 1 Answer

PlayerPrefs not saving 1 Answer

Adding Value to Already Saved Values 1 Answer

Distribute terrain in zones 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges