how retrieve final player position in previous scene after switching from a new scene? in 2d rpg

Can someone help me to solve my problem? After switching from a new scene, i want my player position to be near to the entry point in the previous scene where i switch to a new scene via door (BoxCollider).

Here is my sample code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SwitchingScene : MonoBehaviour
{
    //designing entry and exit point for the scene switching
    public string nextScene;
    public string enter;
    public string exit;


    public void OnTriggerEnter2D(Collider2D col)
    {
        //or (col.CompareTag("Player")
        if (col.gameObject.tag == "Player")
        {
            SceneManager.LoadScene(nextScene);
        }
    }

    // designing entry and exit point for this script:
    // if player position collider with the intial object(enter), it will enter a new scene.
    // if the player went and collide with another object(exit),
    // it will return back to its intial position(enter) from previous scene. (Unload scene?)

}

Can you help me to code this logic or show a better way to retain the player final position in the previous scene? Thank you.

4 Answers

4

The best way is to Store the location of player just before you change the scene!!

if (col.gameObject.tag == "Player")
         {
             Vector2 tempPos = player.transform.postion;
             SceneManager.LoadScene(nextScene);
         }

and simply handle the data between scenes using a static script!!
Click Here

to know more about static scripts

Maybe a better solution than mine, which uses PlayerPrefs ;)

Thanks for the reply. A follow up question, can this code apply to singleton player ? If not, do i need to make duplicate player for each scene i make? (PS : is singleton and static the same thing?) Thank you.

A simple way would be to store the player’s last position in the PlayerPrefs, then load it at the start of the new scene.
2 ways to do this :

Actually i may not recommend using Playerprefs for storing this type of data, PlayerPrefs should only be used for storing essential Information like Scores,settings,names etc!! its better to handle your kind of data inGame instead of storing outside!!

Are you using a Game Manager to persist data between scenes? I think the general method is to make a Game Manager Object and give it a script that you call DontDestroyOnLoad in. You can make data persist between scene calls inside this script and reference it from another script by getting the scripts component as long as the fields (or methods) are made public.

removing the logs and saving the object in an array will fix your performance problems for sure

Thank you for your generous advices, I have found a video on youtube that describe your single prefab door as warp points.

I hope by changing scene, we too also can transform the preferable location of the player between scene.
Hope this would help and please do leave a comment if you meet with any problem.

Thank you.