- Home /
Setting intial value for playerprefs
hi all, sorry if this has been asked already but i'm stuck. im making a game that will have a lives manager and a double jump manager. i have set playprefs values for each of these and it works fine on game view but once i build an apk file it refuses to allow me to play as the lives value i set in the playerprefs isnt working so its setting lives to 0 meaning no lives to play game. below is my code for the relevant parts.
First the Lives and Double Jump managers
using UnityEngine; using System.Collections;
public class LivesManagerScript : MonoBehaviour {
public static int lives = 6;
void Awake()
{
DontDestroyOnLoad (gameObject);
}
void Start()
{
PlayerPrefs.SetInt ("Lives", lives);
}
void Update()
{
PlayerPrefs.SetInt ("Lives", CharacterMove.lives);
}
}
using UnityEngine; using System.Collections;
public class DoubleJumperManagerScript : MonoBehaviour {
public static int dJumpLimit = 30;
void Awake()
{
DontDestroyOnLoad (gameObject);
}
void Start()
{
PlayerPrefs.SetInt ("DoubleJumps", dJumpLimit);
}
void Update()
{
PlayerPrefs.SetInt ("DoubleJumps", CharacterMove.dJumpLimit);
}
}
next is start button
using UnityEngine; using System.Collections;
public class StartButtonScript : MonoBehaviour {
public int lives = 0;
void Awake()
{
lives = PlayerPrefs.GetInt ("Lives");
}
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);
}
}
}
}
}
}
}
the managers are on the scene after the start button is pressed. i tried this on the start button scene and that didnt work either. any help will be appreciated
Have you tried debugging the lives value? What is Character$$anonymous$$ove? is it a character controller? are you sure it contains the proper value of lives and isn't defaulting to zero thus causing your lives to be set to 0 every update call?
are you sure that when you build the scene that contains the object with the Lives$$anonymous$$anager is added to the build?
Yeah it's my character controller. The thing is I figure it needs to be activated on the first scene but when I move it to that it doesn't give a value either. And yes it's added to the build
Answer by smallbit · Jul 28, 2014 at 06:15 AM
//start
PlayerPrefs.SetInt ("Lives", lives);
//awake
lives = PlayerPrefs.GetInt ("Lives");
Awake runs before start, hence you first try to read playerprefs which does not exist yet (it will be created in start). In your case when you read playerprefs you can do like this :
lives = PlayerPrefs.GetInt ("Lives",6);
6 is a default value, in case your prefs does not exist yet, it will be given value of 6 (your value, not the playerprefs - this will still be nonexistent until you call SetInt on it)
Answer by Mirakulous · Jul 30, 2014 at 10:22 AM
ok so this worked thanks. now my problem is it no longer saves values when app is closed and reopened
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How To Add PlayerPrefs Scores? 1 Answer