- Home /
transition between scenes
Hi everybody!
Maybe i is a really stupid question but i'm tortaly new to unity.... I have a scene with abuilding that has an infodesk.The infodesk has a simple button that when pressed a new scene opens and the user can see the screen and interact with it.My prloblem is tha i need to create a button that when the user exits this scene he returns in the previous scene but exactly in the place he was before. Right now he returns in the main scene but in the entrance of my building and not in front of the infodesk...Thank you!
Can't you make a variable which keeps the positioninformation when you leave a scene? When you go back you ask the variable where to place the user?
Answer by Alec-Slayden · May 04, 2011 at 02:03 PM
There's a couple ways to do this.
One way is to save the player's position to a variable on an object, along with DontDestroyOnLoad To keep it in existence in the new scene. This is good especially if you have various forms of data you might like to keep and reference across multiple scenes, or if you want sounds to continue playing, etc.
Another way is to make use of PlayerPrefs to store and retrieve data between scenes. This is particularly useful for communicating data across any scene order, or even temporarily saving information for return visits.
Edit: Clarification of DontDestroyOnLoad
This function is called in a script on the object you want to keep, telling Unity not to delete it, if another scene is loaded.
if you created an empty game object, you could put a script like this on it (javascript here):
static var savedPosition : Vector3;
function Awake(){ DontDestroyOnLoad(transform.gameObject); }
Unity will see your DontDestroy function as soon as the script awakens, and the object and the variable on its script will continue into any scenes that you load afterward.
since the savedPosition variable is static, you can reference it from any script easily, including setting it's value just before loading a scene.
one of the strengths of this method is that DontDestroyOnLoad protects its hierarchy, so you could, in theory, parent other objects to it interactively if you want them to endure to the next scene also.
Thank g=for your answer Alec,
Can you please explain a bit more the how can i implement the first option?I'm not sure how to use the application.Loadlevel with DontDestroy.. $$anonymous$$aybe you can give me an expample?
i'm going crazy with this....i use this function and when i return to my main scene the player seems like falling from the scene...what do i do wrong?
Sounds like a collider issue. $$anonymous$$ake sure it's not sticking through the terrain / floor when he comes back. $$anonymous$$ake sure he's not moving much when he comes back also, because of the problems with moving platforms and the player controller.
Your answer
Follow this Question
Related Questions
Fade Screen Transition After Death 1 Answer
Trouble with "Creating Screen Transitions" UI How To 0 Answers
Where does GUI end up? 1 Answer
how to get a window bigger than screen 0 Answers
Fixing screen resolution 1 Answer