x


Save/load playerprefs

I am making a relatively simple game, but i want to know how to save it. I made it using quite a few scripts and i ave two scenes: main menu and the actual game. I want to save the players position and some variables and then be able to have the option to either load them, or make a new file. It would be nice to be able to save up to three or so files. I heard about playerprefs and learned some stuff about it, but i could only find stuff about highscores.


I tried the playerprefs.getint in a script, it didn't work. here's the script i tried it on, before i put in the playerprefs script. It's supposed to respawn an enemy a certain number of times and then stopped. I just wanted, for now, to save how many times it respawned an enemy:

var Beholder : Transform;
var targetThing : Transform;
var Weapon: Transform;
beholder.thing = targetThing;
beholder.weapon = Weapon;
var deathTimer : int = 0;
var maxRespawns : int;


var respawnCount : int = 0.0;


function Update () 
{

    if(beholder.death)
    {

        deathTimer++;

        if(deathTimer == 50 && respawnCount < maxRespawns)
        {
            deathTimer = 0;
            respawnCount++;
            beholder.death = false;
            var newBeholder = Instantiate(Beholder, transform.position, transform.rotation);
        }
    }

}
more ▼

asked May 22 '10 at 04:52 PM

oz m gravatar image

oz m
126 14 17 25

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

To save player preferences its actually really simple. They are saved between game sessions and are very easy to use.

Examples (Javascript):

//set a string to a player preference called "MyString"
PlayerPrefs.SetString("MyString", "MyValue");
//now retrieve this value
var test : String = PlayerPrefs.GetString("MyString");
//see? it works with ints too.
PlayerPrefs.SetInt("MyInt", 2);

You can find a complete list of functions by (in unitron) Typing PlayerPrefs, highlighting it, and then hitting the search documentation button.

Hope this helps, Christian Stewart

more ▼

answered May 22 '10 at 05:32 PM

user-1846 (google) gravatar image

user-1846 (google)
216 6 6 13

how do i save the player's position and rotation and stuff

May 22 '10 at 05:39 PM oz m

PlayerPrefs only saves very simple types. The easiest way to save the player's position inside PlayerPrefs, for example, is by storing 3 variables for position.x, y, and z.

Jun 05 '10 at 06:30 PM Tetrad

OR, you can make a string like position.x+":"+position.y+":"+position.z and then parse it by PlayerPrefs.GetString("position").Split(":");

Jun 16 '10 at 02:30 AM user-1846 (google)

Note that saving a lot of data to PlayerPrefs can be very slow on Android and iOS devices. To improve this, you can write your own code to save this to a file, or use the following custom PlayerPrefs class we wrote: http://www.previewlabs.com/writing-playerprefs-fast/

Mar 11 '11 at 11:36 AM bernardfrancois
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x436
x434
x294
x176

asked: May 22 '10 at 04:52 PM

Seen: 12899 times

Last Updated: Nov 25 '10 at 06:28 AM