Question by
anwille_555 · Sep 01, 2017 at 07:52 AM ·
transformpositionplayerprefspositioningpositions
Player Prefs not setting the position at start of the game
Hi I have a main menu where I delete my player prefs, and then at my level 1 I set up the player position by getting the float and writing it in the start function.
Vector3 newPos = new Vector3(PlayerPrefs.GetFloat("PlayerX"), PlayerPrefs.GetFloat("PlayerY"), PlayerPrefs.GetFloat("PlayerZ"));
player.gameObject.transform.position = newPos;
the script which save the positions to player prefs is as follows which I wrote at other script in the start functio:
GameMaster.gm.spawnPoint.position = checkpoints[value].transform.position;
PlayerPrefs.SetFloat("PlayerX", GameMaster.gm.spawnPoint.position.x);
PlayerPrefs.SetFloat("PlayerY", GameMaster.gm.spawnPoint.position.y);
PlayerPrefs.SetFloat("PlayerZ", GameMaster.gm.spawnPoint.position.z);
PlayerPrefs.Save();
the problem is when I came from main menu which deletes the player prefs and I load level 1, the player's position is set up at 0,0,0 axis. I think the problem here is the player prefs not setting up at the start function, I tried writing it at the update, but the player's position will be stuck because it is called once per frame. Any help would be highly appreciated! Thank in advance
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612134002im_/https://answers.unity.com/themes/thub/images/avi.jpg)