- Home /
Retaining/restoring position information when loading scenes
Hi all,
I'm pretty new to Unity (but learning fast, thanks to the awesome people here!) and I am having a problem when loading between scenes.
In the game I am making, the player is able to travel to different points from the same base level. The problem I am having is that when the player reloads the main level after visiting one of the side levels, they always appear at the same position - in the center of the map, say.
I know there must be some ability to store the players x,y,z co-ordinates when they leave the level and re-apply them when they return (so that they appear in the same place they were in before they left), but I am a n00b when it comes to coding really so I'm not sure how it would be accessed. Could anyone give an example, or point out any examples of this kind of coding I could learn from?
Answer by KvanteTore · Apr 02, 2010 at 04:28 PM
One way would be to use PlayerPrefs to store the previous position in a level when a new level is loaded. This PlayerPrefs is stored between sessions, so the positions would be remembered the next time the player plays the game.
Another option is to create a GameObject with the purpose of keeping track of the position in the visited levels, and use DontDestroyOnLoad to keep this object from being destroyed when a new level is loaded.
Another option is to store the position in a static variable. These variables persist between scene loads.
I got the static variable system working, thank you for your help :) now I need to look into playerprefs (you can never learn too much!)
Heh. Thanks lucas. Static vars are probabbly the easiest solution :)
Answer by Cyclops · Apr 06, 2010 at 06:19 PM
Here's an example project I posted, which creates a permanent LevelManager that other scripts can access for variables: Simple Demo of a LevelManager.
Thank you very much for this. Im pretty new to coding, so the comments explaining it were a big help to me :)