- Home /
Player.Prefs not working on android[solved]
Basically my Player.prefs control my players Coins and lives it works flawlessly on unity and in a windows build but when i convert it into android id doesn't work at all not even in the same scene i really do not understand where im going wrong
heres my life manager
public class LivesManager : MonoBehaviour {
public int StartingLives;
private int LifeCounter;
private LivesManager LifeSystem;
public GameObject Timer;
public Transform SpawnPoint;
private Text thetext;
void Start()
{
LifeSystem = FindObjectOfType<LivesManager>();
thetext = GetComponent<Text>();
if (PlayerPrefs.GetInt("life") <= 0)
{
StartCoroutine(WaitTime());
}
}
void Update()
{
if (PlayerPrefs.HasKey("life"))
{
LifeCounter = PlayerPrefs.GetInt("life");
}
thetext.text = "x " + LifeCounter;
}
public void GiveLife5()
{
LifeCounter += 5;
PlayerPrefs.SetInt("life", LifeCounter);
PlayerPrefs.Save();
}
public void GiveLife()
{
LifeCounter++;
PlayerPrefs.SetInt("life", LifeCounter);
PlayerPrefs.Save();
}
public void Takelife()
{
LifeCounter--;
PlayerPrefs.SetInt("life", LifeCounter);
PlayerPrefs.Save();
}
public void OnButtonPressChallenge()
{
if (LifeCounter > 3)
{
Debug.Log("Nope");
return;
}
Application.LoadLevel("ChallengeMode");
}
public void OnButtonPressStore()
{
Application.LoadLevel("Store");
}
public void OnButtonPressMission()
{
Application.LoadLevel("Zone1");
}
This is accessed by multiple scripts including this welcome button which adds 5 lives.
public class Welcome : MonoBehaviour {
private int Clicked;
public GameObject Button;
private LivesManager LifeSystem;
void Start()
{
LifeSystem = FindObjectOfType<LivesManager>();
if (PlayerPrefs.HasKey("welcome"))
{
Clicked = PlayerPrefs.GetInt("welcome");
}
}
public void OnButtonPress()
{
Debug.Log("Welcome");
LifeSystem.GiveLife5();
Clicked++;
PlayerPrefs.SetInt("welcome", Clicked);
PlayerPrefs.Save();
}
void Update()
{
if (Clicked == 1)
{
Debug.Log("goneforever");
Destroy(Button);
}
}
}
Answer by saschandroid · Jul 28, 2016 at 06:02 AM
I guess the reason is, if you installed the game on android you don't have any PlayerPrefs when starting the game. So if you use 'HasKey' in Start() it returns false and if you're trying to get a value it returns the default value (e.g. 0, because you didn't set a default value yourself like: GetInt("life", 7)). When playing the game in Editor or Windows you probably already have saved PlayerPrefs from a different version of your game (afaik they don't get deleted if you play in Editor mode or build a new Windows version, but re-installing on Android deletes previously saved PlayerPrefs ... so they are empty again after a new build). Save the PlayerPrefs with your default values once, after installing and starting the game.
I would like the lives to start at 0 upon first start would i still need to set this? and they reset even after welcome button is pressed and scene is changed but welcome button does not reappear.
Answer by Denscrivent · Jul 28, 2016 at 04:21 AM
try debug.log the prefs and use adb logcat to check it when you play the game in android device