- Home /
Saving boolean values using PlayerPrefs
I can't get my "riflepickup" boolean value to save/load using PlayerPrefs
could someone please take a look at my script and see if they can figure out the source of the problem? I really need help as I am completely clueless
cheers
#pragma strict
var player : Transform;
var x : float;
var y : float;
var z : float;
var riflepickup : boolean;
function Start () {
var weaponobj = GameObject.Find("shooting reference");
var weapon = weaponobj.GetComponent(weaponscript);
//if(PlayerPrefs.HasKey("x") && PlayerPrefs.HasKey("y") && PlayerPrefs.HasKey("z") ) {
//load player position
x = PlayerPrefs.GetFloat("x");
y = PlayerPrefs.GetFloat("y");
z = PlayerPrefs.GetFloat("z");
player.position.x = x;
player.position.y = y;
player.position.z = z;
//load rifle pickup true/false
riflepickup = PlayerPrefs.GetInt("riflepickup") > 0;
//}
}
function OnTriggerEnter (col: Collider) {
var weaponobj = GameObject.Find("shooting reference");
var weapon = weaponobj.GetComponent(weaponscript);
if(col.tag == "Player")
{
riflepickup = weapon.riflepickup;
PlayerPrefs.SetInt("riflepickup", weapon.riflepickup ? 1:0);
PlayerPrefs.Save();
x = player.position.x;
PlayerPrefs.SetFloat("x", x);
y = player.position.y;
PlayerPrefs.SetFloat("y", y);
z = player.position.z;
PlayerPrefs.SetFloat("z", z);
Debug.Log ("saved");
}
}
Appear to be ok, you need to debug.
Add some Debug.Log(PlayerPrefs.GetInt("rifle pickup")) before get and set player prefs. And confirm in register that it was correctly updated.
You didn't tell us what problem you are seeing. You simply said it doesn't work. What are you seeing happen?
Use this; it's the same idea for saving booleans, but having SetBool/GetBool makes the code more clear. Also get rid of the x/y/z variables since you don't need them. Just save player.position.x etc. directly, or better yet use Set/GetVector3 from ArrayPrefs2.
@thepunisher it is saying that it cannot convert the int to a boolean value.
cheers guys
Answer by Tanshaydar · May 19, 2014 at 08:09 PM
riflepickup = PlayerPrefs.GetInt("riflepickup") > 0;
Instead of this, use this:
riflepickup = ( PlayerPrefs.GetInt("riflepickup") == 1);
Did you try and talk? Or are you just talking theoretically?
Your answer
Follow this Question
Related Questions
Saving and loading game with PlayerPrefs 1 Answer
Problems with saving/loading score with PlayerPrefs [C#] 1 Answer
Null Reference Exception even after check 1 Answer
Save/load playerprefs 2 Answers
Save and Load Function 2 Answers