- Home /
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 ();
}
}
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 ();
}
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()
.
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.
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