- Home /
Losing prefab transform values on scene open
Hey guys, i'm instantiating a clone of a first person controller in a scene called home, when i start the game, then i go into a scene called city which serves as a portal to other scenes, so far so good i can return to the scene home and the first person controller starts where i want it to, then i go back to the city scene, it also goes where i want it to, i have spawners in each scene that communicate with each other setting the right position for each time the first person controller(player) enters or leaves a scene, so as i was saying when i get out of the home scene into the city scene and then back to the home scene everything goes as supposed to, the problem happens when i go into the city scene and then enter one of the portals present in the city and then go back to the home scene, when i re-enter the home scene the player looses its transform values and is instantiated out of the playable space, and i can't find a solution to setting it to the place it should spawn.
The spawners work as following : the city scene as a master spawner (gameobject-->cube) that runs this script, and other spawners (gameobject-->cubes) which serve as a reference for the spawning position of the player with a simple dont destroy o load script, the master spawner will then move the player to each of the other cubes position
Thanks in advance =)
public GameObject emptyObject ; // Drag empty object here
public bool drop_casa = false;
public bool drop_casino = false;
public bool drop_galeria = false;
public bool drop_mercado = false;
public bool drop_farmacia = false;
public GameObject Player ;
public Transform Destination_casa;
public Transform Destination_casino;
public Transform Destination_galeria;
public Transform Destination_mercado;
public Transform Destination_farmacia;
public bool Esc_casa = false;
public bool Esc_casino = false;
public bool Esc_galeria = false;
public bool Esc_mercado = false;
public bool Esc_farmacia = false;
// Use this for initialization
void Start () {
GameObject player = Instantiate(Player) as GameObject;
DontDestroyOnLoad (this);
}
// Update is called once per frame
void Update () {
if (Esc_casa)
{
drop_casa = true;
Player.transform.position = Destination_casa.position;
Debug.Log("blabla");
}
if (Esc_casino)
{
drop_casino = true;
Player.transform.position = Destination_casino.position;
Debug.Log("bleble");
}
if (Esc_galeria)
{
drop_galeria = true;
Player.transform.position = Destination_galeria.position;
Debug.Log("blibli");
}
if (Esc_mercado)
{
drop_mercado = true;
Player.transform.position = Destination_mercado.position;
Debug.Log("bloblo");
}
if (Esc_farmacia)
{
drop_farmacia = true;
Player.transform.position = Destination_farmacia.position;
Debug.Log("bloblo");
}
DontDestroyOnLoad (this);
}
void OnLevelWasLoaded(int level) {
if (level == 1)
{
Esc_casa = true;
}
if (level == 2)
{
Esc_casino = true;
}
if (level == 3)
{
Esc_galeria = true;
}
if (level == 4)
{
Esc_mercado = true;
}
if (level == 6)
{
Esc_farmacia = true;
}
}
}
Answer by abhi_360 · May 20, 2015 at 11:33 AM
firstly remove DontDestroyOnLoad (this) from Update
then in your void OnLevelWasLoaded(int level) { if (level == 1) { Esc_casa = true; Esc_casino = false; Esc_galeria = false; Esc_mercado = false; Esc_farmacia = false; }
whats happening is your bools are not resetting so if u go by ur script then the more than 1 boolean is true so in ur update loop if the first 3 booleans are true u will be in galeria's spawnPoint and remember in more than 1 levels the spawnPoint may be the same World Space Coordinate like (0,0,0) and in the other levels it might be different which becomes more confusing.
Answer by Flash Voyager · May 20, 2015 at 04:00 PM
So i comented the don't destroy on load (this), and its working i can enter the home scene and then go into the city scene and when i go back to the home scene it spawns the player where it should, but now i get another problem for which i tried the same solution but it doesn't seem to solve it, when i'm in the home scene i have another script similar to the one above and it works fine, but when i go back to the city scene and enter another scene say for example the supermarket scene and then i go back into the city it doesn't load the player where it's supposed to (in front of the supermarket entrance), i find its because of the commented don't destroy on load, with it commented it doesn't check what level was loaded and therefore the boolean doesn't plug on/off
you dont need the DontDestroyOnLoad(this) on update as calling it on start persists the object throughout all scenes watch this
view the video for reference. Your problem is your not resetting all your variables to their default values on changing scenes you need recheck all your scripts that have a DontDestroyOnLoad(this) and Add a Reset function which resets all your variables to default values. If you require some kind of example i'll create a sample Unity project on Git if i have time :)
Your answer
Follow this Question
Related Questions
DontDestroyOnLoad on disabled object doesn't work? 1 Answer
How to disable Dont destroy on load 0 Answers
How to use one object selected from previous scene to the current scene ? 1 Answer
Cannot close/unload a scene that is open in editor during playmode (using C# code)? 3 Answers
Add Menu to Unity Roguelike Example 0 Answers