- Home /
[SOLVED] A Playerpref in one place is blocking all my other Playerprefs
So in fixing one issue, I've come across a whole new (potentially bigger) issue. I've been using Playerprefs to save most everything. (Player health, money, items, etc.) Everything was working fine. I came to the new problem of wanting to save enemy status. In other words, when the player killed an enemy, I wanted the enemy to stay dead. (important for boss characters between game sessions) So after several hours over Skype with a very generous fellow Unity fan we had a working script which did exactly what I needed, the enemy does not come back to life after game sessions. HOWEVER, now nothing that would previously save or load does either. It's almost like the new script is blocking any other Playerprefs from saving / loading. Even the debug log doesn't get called anymore. I'm stumped, and am hoping someone might be able to point me in the right direction. Thanks, and God bless. The scripts are below, starting with the Playerprefs master script, which was previously working, then the new script.
SCRIPT !:
function Start() {
OnLoad();
autoSaveEnable();
}
function autoSaveEnable() {
for(var x = 1; x>0; x++) {
yield WaitForSeconds(5);
Debug.Log("save me");
OnSave();
}
}
// a function created to save a game
function OnSave()
{
PlayerPrefs.SetInt("CurXp", Playerhealth.curXp);
PlayerPrefs.SetInt("MaxXp", Playerhealth.maxXp);
PlayerPrefs.SetInt("MaxHealth", Playerhealth.maxHealth);
PlayerPrefs.SetInt("CurHealth", Playerhealth.maxHealth);
PlayerPrefs.SetInt("Level", Playerhealth.level);
PlayerPrefs.SetInt("Money", Playermoney.curMoney);
PlayerPrefs.SetInt("HardTack", Inventory.HardTack);
PlayerPrefs.SetInt("HealthPotion", Inventory.HealthPotion);
}
// a function created to load a game
function OnLoad()
{
Debug.Log("loaded");
Playerhealth.curXp = PlayerPrefs.GetInt("CurXp");
Playerhealth.maxXp = PlayerPrefs.GetInt("MaxXp");
Playerhealth.maxHealth = PlayerPrefs.GetInt("MaxHealth");
Playerhealth.curHealth = PlayerPrefs.GetInt("CurHealth");
Playerhealth.level = PlayerPrefs.GetInt("Level");
Playermoney.curMoney = PlayerPrefs.GetInt("Money");
Inventory.HardTack = PlayerPrefs.GetInt("HardTack");
Inventory.HealthPotion = PlayerPrefs.GetInt("HealthPotion");
}
SCRIPT 2
static var GuardianHealth : int = 5;
function Start(){
if(PlayerPrefs.GetInt("GuardianDead", 0) == 1){
//object.active = true;
//object.enabled = true;
Destroy(gameObject);
}
}
function OnTriggerEnter (other : Collider){
if (other.gameObject.name == "sword") {
GuardianHealth = -10;
}
else
{
if (other.gameObject.name == "club")
{
GuardianHealth = -1;
}
if(GuardianHealth <= 0 )
{
GuardianHealth = 0;
audio.Play();
PlayerPrefs.SetInt("GuardianDead", 1);
Destroy(gameObject);
}
}
}
I'm fuzzy about your issue. What I see right now is that all the code in the enemy health script is executed in OnTriggerEnter(). At the (re)start of a level, this is not executed. I think you need to make all this code its own function and execute it both in OnTriggerEnter() and in Start().
I briefly thought that last night. Will try it out when I get home.
Answer by Conect11 · Oct 24, 2013 at 12:29 AM
Go figure. Another script WAS blocking the other Playerprefs, but not the script I thought. I had forgotten about my entirely separate EXP script, which gets called on destroy. Unfortunately, since it did, the command never fired to save anything else to Playerprefs. Simply eliminating that script, and placing its contents into the enemy health script solved my issue.
Your answer
Follow this Question
Related Questions
Attempting to store an Inventory Array in PlayerPrefsX 1 Answer
PlayerPrefs saving, but not loading some values 2 Answers
Player Prefs adding XP randomly 2 Answers
Need help with PlayerPrefs 2 Answers