- Home /
Save as many pickables as you want to
Hi guys
So I started to play around with the save and load problem in unity. I'm using PlayerPrefs at the moment, and my question is: Is there a way to save as much pickable objects as you want?
So for example I have 5 different keys, which the player can pick up. But since a good script is modular, if I have 15 keys in the next level, I dont want to add another 10 lines into the saving function to save them. I'm thinking about arrays, but have no idea how to do it.
So what I really want to do is, that each key would check when loading up the game, if it was picked up before. If yes, then disable that key, if not then enable it.
Thanks in advance :)
Answer by oliver-jones · Aug 28, 2013 at 10:34 AM
First of all, I would advise you to check out PlayerPrefsX. It's just like the PlayerPrefs within Unity, but it allows you to save booleans, arrays, vectors, etc ...
Now, I would do something like this, have a save function that you can call, and save a value into it:
function Save(object : String, value : boolean){
PlayerPrefsX.SetBool(object, value);
}
Then, whenever your player picks up a key, you can do this:
Save("key_name", true);
Example:
//Don't have
Save("key1", false);
//Do have
Save("key2", true);
//Do have
Save("key3", true);
//Don't have
Save("key4", false);
And if you do want to use arrays:
SaveArray(KeyIDs[], KeyPickUp[]);
function SaveArray(object : String[], value : boolean[]){
PlayerPrefsX.SetBoolArray(object, value);
}
-------- UPDATE --------
Okay, so you have your keys all over your map okay? So what you need to do is reference them in your script when you have picked them up.
So, I'm guessing you have a script that allows you to pick up your key? And I'm guessing your keys have a Unique Object name? So not, then do that first; So all your keys are named like 'Key_1', 'Key_2' ...
Then, within your pick up script, you want to do something like this:
// we do not have any of the keys
var key_1 : boolean = false;
var key_2 : boolean = false;
var key_3 : boolean = false;
if(pickedUp.transform.name == "Key_1"){
key_1 = true;
Save("key_1", true);
}
else if(pickedUp.transform.name == "Key_2"){
key_2 = true;
Save("key_2", true);
}
If you want to load if you have your keys or not:
function Start(){
key_1 = PlayerPrefsX.GetBool("Key_1");
key_2 = PlayerPrefsX.GetBool("Key_2");
key_3 = PlayerPrefsX.GetBool("Key_3");
}
---------- UPDATE 2 ----------
Or, if you are comfortable with using arrays, you can do this:
//Place all your keys from the scene into here (via Inspector)
var KeyObjects : GameObject[];
//Make all these false from the inspector
var KeyHave : boolean[];
function Start(){
KeyHave = PlayerPrefsX.GetBoolArray("KeyHave");
}
function OnCollisionEnter(hit : collider){
for(var i = 0; i < KeyObjects.length; i++){
if(hit.colldier.transform.name == KeyObjects[i]){
print("We have just walked over key: " + KeyObjects[i]);
KeyHave[i] = true;
//Saves all the keys value
Save("KeyHave", KeyHave);
}
}
}
function Save(name : String, value : boolean[]){
PlayerPrefsX.SetBoolArray(name, value);
}
I already checked out PlayerPrefsX but have no clue how to use Arrays, they are so confusing :D
Anyway, does "key_name" add the gameObjects name to it automaticly, so it numbers it like in your example?
And how could I load the saved thing?
$$anonymous$$an, I'm trying really hard to figure out where to place your Array script but with no success. Does it go to the player or on the keycard. Because you are using OnCollisionEnter. And btw I use E to pick up things.
Your answer
Follow this Question
Related Questions
Unity keeps crashing when I try to save a scene 0 Answers
Save Transform in PlayerPrefs 2 Answers
Saving Player Position Scene to Scene 0 Answers
Web player for my project 0 Answers
How can I save & load gameObjects? 1 Answer