Saving Player Position between scenes?

Hi, I am currently working on building a 2d sprite based RPG game. So far I’ve managed to solve any issues that have come up, but I’ve been working at this one for nearly a week and I am stumped.

The idea is that there is a player sprite you move around the over-world, and whenever it collides with an enemy sprite, a separate battle scene is loaded for a traditional 2d RPG battle, like those in some of the older Final Fantasy games. I can load my battle scene just fine, but whenever the battle ends and the scene closes, the player sprite returns to its original starting position in the over-world. I have two prefabs for the player, one for the player in the over-world and one for the player in the battle scene, since the battle and the over-world require very different scripts for the player to operate. I also have an object that I have named “God” that has a “DoNotDestroyOnLoad” function attached to it, making it persistent between scenes. This object tracks things such as player HP and other stats, and manages some other stuff as well.

What I would like is the game to save the position of the player sprite when it enters the battle, and then after the battle is complete reload the scene with the player at this position. Is this something that can be done with a binary formatter, (which is what I am using to save my data such as player HP and all that) or is there another way that I can do this? I’m honestly not sure what scripts to attach here to reference this, so I’m just going to kind of leave this open ended.

I really appreciate any help that I get in response to this question, thanks in advance to anybody who helps me solve this problem! :slight_smile:

There might be other way to do it but here is what I did for a similar problem: On the objects that need different shader properties, I use the ExecuteInEditMode attribute a the component that manages those shader properties. Doing so, OnEnable (and Update) are called in the Editor too. Creating and assigning a MaterialPropertyBloc during those callbacks in the Editor works well. You just have to be careful to separate your Editor and player/runtime code, using #if UNITY_EDITOR and Application.IsPlaying.

1 Answer

1

I think you could use playerprefs for that. Also I think you could use DontDestroyOnLoad on your player object and hide it in the battle scene, keep it in the background, then activate it again when you switch back.

Hope that helps,
Areleli Games

The DontDestroyOnLoad idea is pretty interesting, I'll look into it to see if I can figure out something with that. Playerprefs could help solve the problem, but what I am wondering is whether or not the same thing could be achieved with a binary formatter. (I've tried that and it didn't really work) You've definitely given me some stuff to think about, though if anybody else has a suggestion please let me know, I'll test this stuff out tomorrow and get back to you to let you know how it works. Thanks!

good answer