Question by
myunity87 · May 11 at 12:45 PM ·
playerprefs
playerprefs
Unity does something weird when i use playerprefs to store an int value if a gameobject is active or inactive in hierarchy. Unity will store the values with correct key and value i checked regedit. but when i load the playerprefs it will load for only one key and one value. so it will set all the gameobjects active or inactive. I dont understand how this is possile since they are stored with different keys and values based on if the gameobjects is active or not.
when i save my playerprefs i use this code:
// save character info
for (int i = 0; i < playerStats.Length; i++)
{
if (!playerStats[i].gameObject.activeInHierarchy)
{
PlayerPrefs.SetInt("Player_" + playerStats[i].charName + "_active", 0);
}
else if (playerStats[i].gameObject.activeInHierarchy)
{
PlayerPrefs.SetInt("Player_" + playerStats[i].charName + "_inactive", 1);
}
PlayerPrefs.SetInt("Player_" + playerStats[i].charName + "_Level", playerStats[i].playerLevel);
PlayerPrefs.SetInt("Player_" + playerStats[i].charName + "_CurrentExp", playerStats[i].currentEXP);
PlayerPrefs.SetInt("Player_" + playerStats[i].charName + "_CurrentHP", playerStats[i].currentHP);
PlayerPrefs.SetInt("Player_" + playerStats[i].charName + "_MaxHP", playerStats[i].maxHP);
PlayerPrefs.SetInt("Player_" + playerStats[i].charName + "_CurrentMP", playerStats[i].currentMP);
PlayerPrefs.SetInt("Player_" + playerStats[i].charName + "_MaxMP", playerStats[i].maxMP);
PlayerPrefs.SetInt("Player_" + playerStats[i].charName + "_Strength", playerStats[i].strenght);
PlayerPrefs.SetInt("Player_" + playerStats[i].charName + "_Defence", playerStats[i].defense);
PlayerPrefs.SetInt("Player_" + playerStats[i].charName + "_WpnPwr", playerStats[i].wpnPwr);
PlayerPrefs.SetInt("Player_" + playerStats[i].charName + "_ArmrPwr", playerStats[i].armrPwr);
PlayerPrefs.SetString("Player_" + playerStats[i].charName + "_EquippedWpn", playerStats[i].equippedWpn);
PlayerPrefs.SetString("Player_" + playerStats[i].charName + "_EquippedArmr", playerStats[i].equippedArmr);
}
when i load my data i use this code:
// load player stats.
for (int i = 0; i < playerStats.Length; i++)
{
if(PlayerPrefs.GetInt("Player_" + playerStats[i] + "_inactive") == 1)
{
playerStats[i].gameObject.SetActive(false);
}
else if (PlayerPrefs.GetInt("Player_" + playerStats[i] + "_active") == 0)
{
playerStats[i].gameObject.SetActive(true);
}
playerStats[i].playerLevel = PlayerPrefs.GetInt("Player_" + playerStats[i].charName + "_Level");
playerStats[i].currentEXP = PlayerPrefs.GetInt("Player_" + playerStats[i].charName + "_CurrentExp");
playerStats[i].currentHP = PlayerPrefs.GetInt("Player_" + playerStats[i].charName + "_CurrentHP");
playerStats[i].maxHP = PlayerPrefs.GetInt("Player_" + playerStats[i].charName + "_MaxHP");
playerStats[i].currentMP = PlayerPrefs.GetInt("Player_" + playerStats[i].charName + "_CurrentMP");
playerStats[i].maxMP = PlayerPrefs.GetInt("Player_" + playerStats[i].charName + "_MaxMP");
playerStats[i].strenght = PlayerPrefs.GetInt("Player_" + playerStats[i].charName + "_Strength");
playerStats[i].defense = PlayerPrefs.GetInt("Player_" + playerStats[i].charName + "_Defence");
playerStats[i].wpnPwr = PlayerPrefs.GetInt("Player_" + playerStats[i].charName + "_WpnPwr");
playerStats[i].armrPwr = PlayerPrefs.GetInt("Player_" + playerStats[i].charName + "_ArmrPwr");
playerStats[i].equippedWpn = PlayerPrefs.GetString("Player_" + playerStats[i].charName + "_EquippedWpn");
playerStats[i].equippedArmr = PlayerPrefs.GetString("Player_" + playerStats[i].charName + "_EquippedArmr");
}
Comment
Your answer