- Home /
How Move Player between Scenes?
Okay so I made this script To Enter Level2
// Use this for initialization
void OnTriggerEnter(Collider FPSControllerr)
{
if (FPSControllerr.gameObject.tag == "Player")
{
DontDestroyOnLoad(FPSControllerr.gameObject);
Application.LoadLevel("Level2");
}
}
And its somewhat working. But when I try to go back
// Use this for initialization
void OnTriggerEnter(Collider FPSControllerr)
{
if (FPSControllerr.gameObject.tag == "Player")
{
DontDestroyOnLoad(FPSControllerr.gameObject);
Application.LoadLevel("MainLevel");
}
}
I get 2 FPScontrolers (which is my player) and the game crashes :( Any solutions ?
$$anonymous$$gest avoiding answers that use DontDestroyOnLoad with Unity 5.3+: according to documentation for multi-scene editing:
It is recommended to avoid using DontDestroyOnLoad to persist manager GameObjects that you want to survive across scene loads. Ins$$anonymous$$d, create a manager scene that has all your managers and use Scene$$anonymous$$anager.LoadScene(, LoadScene$$anonymous$$ode.Additive) and Scene$$anonymous$$anager.UnloadScene to manage your game progress.
This calls out manager objects specifically, but I think that's just an artifact of the type of objects typically previously, back when Scenes were called Levels; the main things people would persist were manager objects. With the flexibility of multiple-scene/additive Scene loading, all kinds of objects are now being used across scene boundaries, and this bit of advice applies to any objects you'd want to use across scene boundaries.
Answer by Glonojad · Mar 23, 2016 at 04:48 PM
You can use the SceneManager to move GameObject between scenes.
if (FPSControllerr.gameObject.tag == "Player")
{
Scene sceneToLoad = SceneManager.GetSceneByName("Level2");
SceneManager.LoadScene(sceneToLoad.name, LoadSceneMode.Additive);
SceneManager.MoveGameObjectToScene(FPSControllerr.gameObject, sceneToLoad);
}
did not work I don't know what I'm suppose to do ... ?
using UnityEngine; using System.Collections;
public class ExitAsylum : $$anonymous$$onoBehaviour {
// Use this for initialization
void OnTriggerEnter(Collider FPSControllerr)
{
if (FPSControllerr.gameObject.tag == "Player")
{
Scene sceneToLoad = Scene$$anonymous$$anager.GetSceneByName("Horror"); //Horror is my main level
Scene$$anonymous$$anager.LoadScene(sceneToLoad.name, LoadScene$$anonymous$$ode.Additive);
Scene$$anonymous$$anager.$$anonymous$$oveGameObjectToScene(FPSControllerr.gameObject, sceneToLoad);
}
}
}
by the way why you need to keep player between scenes you can add the player to each scene this is what been done by all developers it's weird to keep it because if you don't destroy you still have to make it a prfab & drooped in to all scenes so it's position will be determent.
holy sh*t man .. that's actually really good idea , but still I need him to be in front of the doors when I get out of the house , which is not the starting position of the player..
Scene$$anonymous$$anager.$$anonymous$$oveGameObjectToScene was exactly what I was looking for! Thanks!
Answer by RomanLed · Oct 07, 2016 at 09:22 PM
Don't use Don'tDestroy, use additiveSceneLoading... this is how.... https://www.youtube.com/watch?v=S9h_iu4Zx5I&feature=youtu.be
Answer by Detinator10 · Mar 23, 2016 at 09:26 PM
I would just have the player's position set to the door when it loads the new scene. So have it use DontDestroyOnLoad and then just run a function after you change scenes that places your character wherever it needs to be. That would be the simplest way to do it.
Another thing, if your player is still getting duplicated, you could briefly transition to a blank scene with the player object when your game starts, set the player to not destroy and then load the regular scene. That way there isn't a player game object as part of your scene, only one from a different scene that you never return to
okay lets say I have 3 different houses and I want to enter the 1st and get out in front of it and then enter the second and get out in front of it ... is there a possible script to move player when I get out of certain scene ? @ali-hatem @Detinator10
Answer by Eudaimonium · Mar 24, 2016 at 10:00 PM
I wrote an article on this particular problem a while back.
DontDestroyOnLoad needs a bit more work.
http://www.sitepoint.com/saving-data-between-scenes-in-unity/
Answer by Ali-hatem · Mar 23, 2016 at 04:25 PM
attach a script to the player to keep it active & delete the don't destroy from your script above & in player script you only need this :
void Start(){
DontDestroyOnLoad(gameObject);
}
should I make variable before ? I'm new to scripting and I really cant seem to get it ...
so did this worked with you & if you need variables to persist so you don't lose score or something when switch to another scenes add variables to this script & access it from another scripts because every thing in this script will not be destroyed & lastly does player will be destroyed in someway by enemy or something .
did not work... my point is to to enter a door and the next scene is the house inside , after that I want to get out of the door and to be in the "main scene" but infront of the house not at my starting position :/