- Home /
Save game before Android app closed?
Hi!
I want save some variable (score, gameObject position,...) if the player quit the application. My idea is to save this variable automatically. Even if the player quit the app with the "home button" of the mobile". I can do this in the update, but it will not be very optimized.
Be sympathetic with my poor English.
Answer by Viperia · May 29, 2014 at 11:21 AM
Yes. My script:
using UnityEngine;
using System.Collections;
public class SaveGameOnQuitScript : MonoBehaviour {
// Use this for initialization
void Start () {
OnApplicationQuit ();
}
// Update is called once per frame
void Update () {
}
void OnApplicationQuit() {
PlayerPrefs.SetInt ("score", ScoreScript.score);
}
}
I've tried, first, without the "OnApplicationQuit()" in the Start method=>don't work I've tried, first, without the "OnApplicationPause(bool pauseStatus)" =>don't work
NB: ScoreScript.score works perfecly in the others scripts of the game. NB: It work perfectly in Unity but not on my device. NB: The OnApplicationFocus function works on my device
I have the same problem, the events won't fire when the user quits the app on Android. For now I need to periodically save the game state, which seems like a stupid way to do it.
Answer by fafase · May 29, 2014 at 10:04 AM
Have you tried this?
http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationQuit.html
Also, usually when you press the home button, only the OnApplicationPause event will fire.