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