Unity2D - How do I position the player according to where I entered the scene?

I’m making a side-scroller 2d pixel game and I came across a problem that i have no idea how to solve. Whenever I change scene I use SceneManager.LoadScene() and that works great, I can enter a different area in the game. However, since I’ve put the player on the left side of each scene, no matter from where you enter the scene, the player will always be on the left side of the area. For example. I go to the area to the right of the spawn area and nothing looks wrong. When I go back to the previous area the player is at the left instead of being at the right of the scene which would be the most logical considering you entered from that side. How can I fix this?

Have you thought about making a start position script for it?

So, I take it that on each side of the scene, you have a trigger, that loads another scene.

My solution would be, creating a static class, which would store… for example an ID of a spawnpoint. Static classes don’t change between Scenes, so they can hold this information for you.

Now, once you load a new scene, you would have to get the ID that was stored inside that static class, and position the player accordingly.

public static class InfoToMaintain{

	public static int id;
}

That’s how you create a static class. Every variable inside it has to be static as well, and it shouldn’t inherit from MonoBehaviour. Anyway, basically, your trigger, before loading up a new scene should set the ID inside this class to something.

Because it’s a static class, you don’t have to declare it, so just writing InfoToMaintain.id = 1 would work.

Now, once you’ve loaded up the new scene, you might have an array that contains all the possible spawnpoints inside, so you would just need to set the player position to that as well.

public class Player{
	// It could be a simple Vector3 array, storing the positions for the spawnpoints.
	public Transform[] spawnPoints;
	
	void Start(){
		// If this script is attached to a player, then we can directly get the transform of the player. If it's not - you would have to find the player first.
		transform.position = spawnPoints[InfoToMaintain.id].position;
		// Here, we use the ID that we set, to get the position of the spawnpoint and setting players positions to its position.
	} 
}

I do hope this was clear enough for you to understand. If anything, hit me up and I’ll try to help you out.