Tracking game object status and position across multiple scenes

Hi All

I have a couple of scenes.

Scene 1 contains a few boxes which can be shot. These boxes (prototype enemies) have healthpoints and when shot their healthpoints decrease, they’re also moved by the impact (AddForce).

In this scene I also have a trigger which loads you to scene 2 when you stand on it.

Scene 2 is just an empty scene with the exception of a trigger which loads scene 1 again.

What I want to do is shoot some enemies / boxes to kill them, and move them around the level a bit - then enter the trigger to scene 2, then enter the trigger in scene 2 to go back to scene 1 and have the boxes in the same position as they were when I went to scene 2 (i.e. not in their default positions). I also want their healthpoints/status to persist.

I’m just wondering what the theory is behind doing something like this?

Would I have to store the position of each object in a static game control object (for example). Would I have to write a bit of code for every object? This would be ok for a few objects but could become unwieldy for a bigger game.

Would it be possible to get the position and healthpoints of all objects with a certain tag/layer when switching scene and store this data in a file which can be read when loading the scene again?

Any advice would be greatly appreciated. Thanks

You could keep the data you want saved in classes, then perhaps in a list, also.
You could have an empty game object with DontDestroyOnLoad that you use to keep these variables alive.
When you load back to scene 1, you can traverse your list and setup your enemies based on the info you previously stored. (this could mean just resetting their positions & health, but it could also mean a different number of enemies compared to the normal load - ie: more or less … extras spawned and/or more died off**.
I think that’d work okay.

Yes, you could write to disk & read back. Yes you could use static variables and look it up, and another option would be playerprefs… but as you said, as the enemies grow, that’s not as easy, in my opinion. Saving to disk could be okay for enemies, if you wanted to save between play sessions (like closing the game, re-running it).

static & playerprefs would be cool for other situations. Unless, of course, say you have a limit of maybe 10 enemies. Then, you could just use either of those, I’d guess. Not a big deal. :slight_smile:

Edit: I actually want to adjust my post slightly. For a list of enemies, I think a static list of data would be perfectly okay. I’d still shy away from PlayerPrefs, for this.

Hope that helps.

Thanks Methos! :slight_smile:

You’re welcome :slight_smile: