saving player prefs when app is closed

Hi guys carrying on from last questions.

so i now have the values of doubles jumps and lives being saved when retry button is pressed. but when the game is closed and reopened the values reset. how do i get this to save when the game is closed.
code below

Lives Manager:
public class LivesManagerScript : MonoBehaviour {

public static int lives = 6;

void Awake()
{
	DontDestroyOnLoad (gameObject);
	PlayerPrefs.SetInt ("Lives",lives);
}

void Update()
{
	lives = PlayerPrefs.GetInt ("Lives");
}

}

DoubleJump Manager:
public class DoubleJumperManagerScript : MonoBehaviour {

public static int dJumpLimit = 30;

void Awake()
{
	DontDestroyOnLoad (gameObject);
	PlayerPrefs.SetInt ("DoubleJumps", dJumpLimit);
}

void Update()
{
	dJumpLimit = PlayerPrefs.GetInt ("DoubleJumps");
}

}

public class StartButtonScript : MonoBehaviour {

public int lives = 0;
public int dJumps = 0;

void Start()
{
	lives = PlayerPrefs.GetInt("Lives");
	dJumps = PlayerPrefs.GetInt ("DoubleJumps");
}

void Update()
{
	if (Input.touches.Length <= 0) {
		
	} 
	else 
	{
		for(int i = 0; i < Input.touchCount; i++)
		{
			if(this.guiTexture.HitTest (Input.GetTouch (i).position))
			{
				if(Input.GetTouch(i).phase == TouchPhase.Ended)
				{
					if(lives > 0)
					{
						Application.LoadLevel (1);
					}
				}
			}
		}
	}
}

}

Player Control

public class CharacterMove : MonoBehaviour {

public float speed = 6.0f;
Transform groundCheck;
private float overlapRadius = 0.2f;
public LayerMask whatIsGround;
private bool grounded = false;
private bool jump = false;
public float jumpForce = 700f;
private bool doubleJump = false;
public int sJumpsCompleted = 0;
public static int dJumpLimit = 0;
public static int lives = 0;


void Awake()
{
	rigidbody2D.fixedAngle = true;
}

void Start()
{
	groundCheck = transform.Find ("groundcheck");
	dJumpLimit = PlayerPrefs.GetInt ("DoubleJumps");
	lives = PlayerPrefs.GetInt ("Lives");
}

void Update()
{
	if (Input.GetMouseButtonDown (0)) 
	{
		jump = true;
		sJumpsCompleted++;
	}
	if (sJumpsCompleted > 9) 
	{
		dJumpLimit = dJumpLimit + 1;
		PlayerPrefs.SetInt ("DoubleJumps", dJumpLimit);
		sJumpsCompleted = 0;
	}
}

void FixedUpdate()
{

	//check if character is grounded
	grounded = Physics2D.OverlapCircle (groundCheck.position, overlapRadius, whatIsGround);

	//reset doublejump when player is on the ground
	if (grounded)
		doubleJump = false;
	//deactivate double jump when no jumps are available
	if (dJumpLimit < 1)
		doubleJump = true;

	//determine if player can jump
	bool canJump = (grounded || !doubleJump);

	if (jump && canJump) 
	{
		rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x,0);
		rigidbody2D.AddForce(new Vector2(0, jumpForce));
		if(!grounded && DoubleJumperManagerScript.dJumpLimit > 0)
		{
			doubleJump = true;
			dJumpLimit--;
			PlayerPrefs.SetInt ("DoubleJumps", dJumpLimit);
		}
	}

	jump = false;

	//apply forward movement
	rigidbody2D.velocity = new Vector2 (speed, rigidbody2D.velocity.y);

}

}

and retry button

public class RetryButtonScript : MonoBehaviour {

void Update()
{
	DoubleJumperManagerScript.dJumpLimit = CharacterMove.dJumpLimit;
	LivesManagerScript.lives = CharacterMove.lives;

	if (Input.touches.Length <= 0) {
		
	} 
	else 
	{
		for(int i = 0; i < Input.touchCount; i++)
		{
			if(this.guiTexture.HitTest (Input.GetTouch (i).position))
			{
				if(Input.GetTouch(i).phase == TouchPhase.Ended)
				{
					if(CharacterMove.lives > 0)
						Application.LoadLevel (1);
				}
				else if(CharacterMove.lives <0)
				{
					Application.LoadLevel (0);
				}
			}
		}
	}
}

}

so basically what happens is when game is started initial value for lives is 6 and double jumps is 30

when player dies or uses double jump values decrease accordingly and are saved and when retry button is pressed they are shown. however when app is closed and reopened the values reset to 6 and 30. i need to get them to be saved so that when app is reopened they don’t reset.

any help is appreciated.

thanks

If i understood the question well this is what you are looking for.You can use OnApplicationQuit() to write the values to playerprefs and read on next launch.

void OnApplicationQuit() 
{
    PlayerPrefs.SetInt("RemainingLives", Lives);
    PlayerPrefs.SetInt("JumpValues", JumpVals);
}

Then you can read this value on start/Awake for next launches

void LoadSaveData()
{
   //Load the lives value.If it doesnt exist give a value 6
   PlayerPrefs.GetInt("RemainingLives", 6);
   //Load the Jump value.If it doesnt exist give a value 30
    PlayerPrefs.GetInt("JumpValues", 30);
}

I think you have to call this:

void OnApplicationQuit() 
{
        //Your data
	PlayerPrefs.Save();
}

thanks sethuraj. i changed it to the following as the names of the variables needed to match mine :slight_smile: but works perfectly now. if you want to put it as an answer rather than a comment i can mark you answer as correct

void OnApplicationQuit()
{
PlayerPrefs.SetInt(“Lives”, CharacterMove.lives);
PlayerPrefs.SetInt(“DoubleJumps”, CharacterMove.dJumpLimit);
}

void LoadSaveData()
{
//Load the lives value.If it doesnt exist give a value 6
PlayerPrefs.GetInt(“Lives”, lives);
//Load the Jump value.If it doesnt exist give a value 30
PlayerPrefs.GetInt(“DoubleJumps”, doublejumps);
}

I have question. Can I save float same as int?

void OnApplicationQuit() { PlayerPrefs.SetInt("RemainingLives", Lives); PlayerPrefs.SetInt("JumpValues", JumpVals); }

Another

void LoadSaveData() { PlayerPrefs.GetInt("RemainingLives", 6); PlayerPrefs.GetInt("JumpValues", 30); }

Can I use .SetFloat or something?
Thanks!