Changing character position when changing scenes.
Hi, I am having problems with my top down 2D game trying to do the following when changing scenes, its a little hard to explain but I'll try and make it sound simple:
There is a Scene called Scene1 with multiple doors, and the player spawns standing in the middle of the room.
I enter one of those doors and it takes me to another scene (Scene 2).
In this scene there is a door behind the player, which would take the player back to the original room (Scene 1).
The problem is when I enter back into Scene1 I want the player to stand in front of the door it had just gone through, instead of just displaying the character in the middle of the room again.
Is there anyway someone could lead me into the right direction for fixing this problem as I could not find any video to solve this...?
Thanks :)
Answer by ray2yar · Jan 26, 2019 at 03:37 AM
I'm going to assume you have a player object in the scene as opposed to the same one persisting between scenes... If so... here's what you do...
Have a gameobject that does persist between scenes.... Something like this:
public class GameManager : MonoBehaviour
{
public static GameManager Instance;
public Vector3 SpawnLocation = new Vector3.zero;
void Start()
{
if(Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
} else
{
Destroy(gameObject);
}
}
On your player object in Start... add this:
void Start()
{
transform.position = GameManager.Instance.SpawnLocation;
}
Whenever the player enters a door... in a function that trips when this has happened... (perhaps in the same function that causes the next scene to load... Add this:
public class DoorHandler: MonoBehaviour
{
public Vector3 SpotInNextRoom;
public void OnDoorEntered()
{
GameManager.Instance.SpawnLocation = SpotInNextRoom;
}
}
Hey thanks for your help! however I have run into a few problems:
I see in your Game$$anonymous$$anager class you stated "if(Instance = null)" I assume that it was just a little mistake and changed it to an == sign. (Correct me if I am wrong)
Also within the player object I am getting an error from unity*: NullReferenceException: Object reference not set to an instance of an object* I have a feeling its to do with the fact that I am using a protected override void Start() ins$$anonymous$$d of just a start function:
protected override void Start() { transform.position = Game$$anonymous$$anager.Instance.spawnLocation; health.Initialize(initialHealth, initialHealth); base.Start();
}
Do you possibly know how you could work around this???
Yes it should be ==
Is the Game$$anonymous$$anager object in the scene?
Check the script execution order - in the first scene if the player's script executes first then it'll throw the null error.... A way around this is to have the first instance of your Game$$anonymous$$anager in your title screen scene (if you have one). Otherwise you'll have to manually tell Unity to execute the Game$$anonymous$$anager script first.
You shouldn't need to be doing anything special with your Start... it's simple .... void Start() - that's it.
There might be another problem... the variable Vector3 SpawnLocation doesn't have a value in the first scene... you'll need to give it one...
you could default: Vector3 SpawnLocation = Vector3.zero;
Sorry, I somehow fixed it whilst I was messing around with the code just now lol. I have no idea what I done but it works.Thanks again for all the help. :)
Really good explanation This helped me so much thanks heaps!!!
Answer by Toddweiss · Aug 07, 2021 at 03:03 AM
If anyone is having issues with this add this to your Player Script
if (Scene_Manager.Instance != null) { transform.position = Scene_Manager.Instance.SpawnLocation; } }
it will fix the error that you player is throwing off