- Home /
PlayerPrefs saving, but not loading some values
I do use PlayerPrefs for everything (even knowing that it should be better to use XML), but it just doesn't works correctly! I mean, I have these attributes:
private int stamna;
private int health;
private float speed;
private float speed; //I have one script to move on X and one to move on Z axes.
private float defence;
private float dmg;
And also have the variables that display the points applyed to each skill during game, each one of them I use at SIX objects, and save individually, since I have a Helmet, a Chestplate, Gloves, Weapon, Pants, Boots:
private int manaPoints;
private int healthPoints;
private int speedPoints;
private int defencePoints;
private int damagePoints;
private int pointsToSpend;
private int currentXP;
private int xpToNxtLvl;
I save them like this:
Equipment:
PlayerPrefs.SetInt(save_load.HeroName + gameObject.name + "SkillKey", theSkill);
Example:
PlayerPrefs.SetInt("*GalahadHelmetDamageKey", damagePoints);
Player stats:
PlayerPrefs.SetInt(save_load.HeroName + "StatKey", theStat);
Example:
PlayerPrefs.SetInt("*GalahadDamageKey", dmg);
And I load them with PlayerPrefs.GetInt (in the case of speed I use GetFloat). Some of the values load and some doesn't:
//Stamna doesn't load correctly, starting with always 100;
//Health is the same of stamna;
//Speed load as 120000 and is unchangeable, what is extremelly wrong, as I should be able to increase 1000 of points each upgrade;
//Defence = 0;
//Damage = 1;
Even if I save their data manually, once I stop the game and run it again they NEVER load correctly, only when I load it by myself. Same trouble with equipments.
REGEDIT folder:
If you need more details let me know!
PS: I'm calling the Load () method once at LateUpdate, and then it works.
you are saving string in playerPrfes and you are using PlayerPrefs.SetInt(save_load.HeroName + gameObject.name + "Skill$$anonymous$$ey", theSkill);. you are setting string in integer, you can get playerprefs as you set. Use Correct format for saving playerprefs. If You are setting float in PlayerPrefs.SetInt then you can't get PlayerPrefs by PlayerPrefs.GetInt().
Answer by KukadiyaPrince · Apr 26, 2017 at 04:59 AM
public class Score : MonoBehaviour
{
public int score = 20;
void Start()
{
PlayerPrefs.SetInt("Score",score); //Save score value
}
//Get Save value
public void UpdateScore()
{
int newScore = PlayerPrefs.GetInt("Score");
Debug.Log("Score" + newScore);
}
}
@$$anonymous$$ukadiyaPrince I do something like this, loading on Awake() and Update(only once using a bool, I added this after I posted the question, and it fixed my issues!) and I set the values at another function, Save(). I load the scripts calling a Load() method, from each script that needs to be loaded, at the Save_Load script. Example:
public class Score: $$anonymous$$onoBehaviour
{
private int score;
private Save_Load save_load;
void LateUpdate ()
{
save_load = GetComponent<Save_Load>();
}
public void Save()
{
PlayerPrefs.SetInt(save_load.HeroName + "Score$$anonymous$$ey", score);
}
public void Load()
{
save_load = GetComponent<Save_Load>();
score = PlayerPrefs.GetInt(save_load.HeroName + "Score$$anonymous$$ey", 0);
}
}
Now an example of the Save_Load script:
public class Save_Load: $$anonymous$$onoBehaviour
{
private string heroName;
public string HeroName { get { return heroName; } }
private bool isLoaded;
private Score score;
void Awake ()
{
Load();
}
void LateUpdate ()
{
LoadScripts();
if (!isLoaded)
{
Load();
isLoaded = true;
}
}
void LoadScripts ()
{
score = GetComponent<Score>();
}
public void Save ()
{
score.Save();
}
public void Load ()
{
LoadScripts();
heroName = gameObject.name;
score.Load();
}
}
It's like this, just in a bigger scale, with a bunch of scripts at Save_Load, each one saving up to 6 values at the same time...
Answer by anthot4 · Apr 28, 2017 at 05:48 AM
@Galahad_Hohan . Im not sure why it isn't working. If you use PlayerPrefs.SetInt etc it will save them automatically even if you quit the application and PlayerPrefs.GetInt will load it. My only thought is maybe you have placed them in the wrong function? Or something like that.
Your answer
Follow this Question
Related Questions
How to get WebGL PlayerPref values out of Indexeddb? 0 Answers
PlayerPrefs: loading saved data from the menu? 1 Answer
Problems with saving/loading score with PlayerPrefs [C#] 1 Answer
[Solved] PlayerPrefs Vector 3 Defaulting to 0,0,0 1 Answer
Need help with using Player Prefs to save number of coins collected. 1 Answer