x


Playerprefs save player position

i was trying to make a alternative script to "this"

here's the script, the GUI's works but it isn't saving, would be happy if somebody could help:)


var playerX : float = 2119.7;
var playerY : float = 20.12;
var playerZ : float = 2770.62;
var playerPosition : Transform;
var player : GameObject;
var inRange = 0;
var onSave : boolean = false;

function Awake () {
    PlayerPrefs.SetFloat("playerX",playerX);
    PlayerPrefs.SetFloat("playerY",playerY);
    PlayerPrefs.SetFloat("playerZ",playerZ);
}

function Start () {
    playerPosition.transform.position.x = (PlayerPrefs.GetFloat("playerX"));
    playerPosition.transform.position.y = (PlayerPrefs.GetFloat("playerY"));
    playerPosition.transform.position.z = (PlayerPrefs.GetFloat("playerZ"));
}

function Update () {        
    playerX =(playerPosition.transform.position.x);
    playerY =(playerPosition.transform.position.y);
    playerZ =(playerPosition.transform.position.z);

    if(onSave == true){    
        PlayerPrefs.SetFloat("playerX",playerX);
        PlayerPrefs.SetFloat("playerY",playerY);
        PlayerPrefs.SetFloat("playerZ",playerZ);
    }
}

function OnTriggerEnter () {    
    inRange = 1;    
}

function OnTriggerExit () {    
    inRange = 0;    
}

function OnGUI () {    
    if(inRange == 1){
        if(GUI.Button(Rect(300,40,400,50), "SAVE!!!")){
            onSave = true;
        }
    }    
}

function loadstuff () {
    playerPosition.transform.position.x = (PlayerPrefs.GetFloat("playerX"));
    playerPosition.transform.position.y = (PlayerPrefs.GetFloat("playerY"));
    playerPosition.transform.position.z = (PlayerPrefs.GetFloat("playerZ"));    
}
more ▼

asked Jun 26 '12 at 11:09 AM

FSeahawk gravatar image

FSeahawk
1 1 2 3

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

3 answers: sort voted first

hey,

you should add

PlayerPrefs.Save();

to your Update's if, right below the .SetFloats() so unity writes the coords in the PlayerPrefs

more ▼

answered Jun 26 '12 at 01:26 PM

Tillomaticus gravatar image

Tillomaticus
0

That's not recommended during game play and is just belt and braces

Jun 26 '12 at 01:31 PM whydoidoit

whydoidoit isabsolutely right suggesting that the save button should just actually save, not set the boolean flag.

Jun 26 '12 at 01:36 PM tomekkie2
(comments are locked)
10|3000 characters needed characters left

It looks to me like your problem is that as soon as you press the button it keeps on saving every frame. Turn off onSave in the Update.

if(onSave == true){
       onSave = false;
       PlayerPrefs.SetFloat("playerX",playerX);
       PlayerPrefs.SetFloat("playerY",playerY);
       PlayerPrefs.SetFloat("playerZ",playerZ);    
}

Or even better:

function OnGUI () {    
    if(inRange == 1){
        if(GUI.Button(Rect(300,40,400,50), "SAVE!!!")){
                 PlayerPrefs.SetFloat("playerX",playerX);
                 PlayerPrefs.SetFloat("playerY",playerY);
                 PlayerPrefs.SetFloat("playerZ",playerZ);   
        }
    }    
}
more ▼

answered Jun 26 '12 at 01:32 PM

whydoidoit gravatar image

whydoidoit
33k 11 23 99

ah forgot about that :D

Jun 26 '12 at 01:44 PM FSeahawk

Please mark the question as answered by clicking the tick next to my answer :)

Jun 26 '12 at 03:08 PM whydoidoit
(comments are locked)
10|3000 characters needed characters left

I updated my script here.

now it's a copy and paste working example.

more ▼

answered Jul 13 '12 at 03:44 AM

kurotatsu gravatar image

kurotatsu
58 1 5 5

(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:

x840
x663
x436
x294

asked: Jun 26 '12 at 11:09 AM

Seen: 654 times

Last Updated: May 02 at 11:23 AM