- Home /
player prefs issues
ok so after getting a fix for saving playerprefs using OnApplicationQuit(). i was able to get this to work once. when player runs of lives however and data is cleared to allow lives to be reset, i am met with 0 lives so game will not run. in unity runtime i am met with same issue and it no longer lets me play.
i'll paste code below.
LivesManager
public class LivesManagerScript : MonoBehaviour {
public static int lives = 6;
void Awake()
{
DontDestroyOnLoad (gameObject);
PlayerPrefs.GetInt ("Lives",lives);
}
void Update()
{
lives = PlayerPrefs.GetInt ("Lives");
}
void OnApplicationQuit()
{
PlayerPrefs.SetInt("Lives",CharacterMove.lives);
}
}
Start Button
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);
}
}
}
}
}
}
}
Character 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);
}
}
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 < 1)
{
Application.LoadLevel (0);
}
}
}
}
}
}
so basically i just want be able to have the lives work as they did and when lives run out to be able to reset the values for testing.
ok so after rewriting code to go with your suggestion i found where the problem was. so would like to ask if you want to put it as an answer i will gladly mark it as correct
Answer by gjf · Jul 31, 2014 at 09:17 AM
this isn't an answer because i don't know where to begin.
it seems like you're using PlayerPrefs to access variables from other scripts rather than getting a reference to a single copy of them using GetComponent<>(), etc.
you should think about rewriting the code to do that - you'll have a lot less bugs, the code will be cleaner, easier to understand, and easier to maintain.
by all means, store variables that you want to persist between session using PlayerPrefs, but it's not intended for doing what you appear to be using it for.
also, your start button and retry button scripts are almost identical - consider using the same script for both and handle the loading separately. try to have all the loading done in one place too... for the same reason as accessing your variables from one place.
I have a question similar to this which you may be able to answer. Please help- http://answers.unity3d.com/questions/874708/save-and-retrieve-score-not-working.html
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Playerprefs not saving 1 Answer
How to use PlayerPrefs to activate game objects permanently? 1 Answer