PlayerPrefs failed to update the previously saved positions

I was trying to use Json and PlayerPrefs to save the final position of my player before quitting the play mode with the code logics in the following:
![8840461–1204486–9}1`FXC7Y%SDLE_)06ADV5.png|1892x912

The position of my player never changed after I re-entered the play mode as the player still started from the original position.

The core of the code logic is that when I quit the play mode, the previous position should be saved and reloaded after I re-enter the play mode.

You might want to manually write changes to disk:
Unity - Scripting API: PlayerPrefs.Save (unity3d.com)

How come?

How so?

Because the value is never written to the registry unless you call PlayerPrefs.Save().

How to use PlayerPrefs.Save()? Where do i need to put it in my code?

After the SetString().

I just tried but the issue still persists…

The position stored in playerprefs is up-to-date but the player never moves to the latest saved position when the play mode starts

Then either:

  • JsonSave isn’t on your player, and its position is always 0
  • You’re clearing PlayerPrefs somewhere
  • JsonSave isn’t even in your scene
    or something similar.

If the OnEnable runs, then it will, unless you’re overwriting it it another script.

I just gave a shot at another way of coding, the playerprefs has the registry of the latest position saved but the player still failed to start at the position saved in the last play mode.

![8840905–1204558–MVRP]%XLZO{[X}MHZL9I7]P.png|2034x913](upload://i7bRLmrQ3Z8p5CXqUaNWsn9tphx.png)

![8840905–1204564–CIGBEHN]7UJO6~3FN7H3WX9.png|1127x732](upload://wpI8VVj9eMXF1R2N7Z9gucanFTn.png)

The saving process will be done by a button click with the method SaveGame() running, the data in the playerprefs is pushed to the registry but the player still failed to start at the position last saved.

Have you debugged what the value written and read from player-prefs is?

What kind of character controller is this, too? Components like the UnityEngine.CharacterController component need to be disabled before you can alter their transform’s position.

It’s also worth noting player prefs really shouldn’t be used for this save-game information.

Then, what is the ideal way to create a save system without using player prefs

Cheers mate, the problem has been solved, I just need to disable the character controller

‘Ideal’ is relative to the project. One project’s save system can be completely different to another project.

But it generally involves serialising data to JSON or similar and writing that out to a file on disk. There are no shortage of tutorials on how to do this.

Exactly, the self-created ones are those most appropriate