1
\$\begingroup\$

I am making a dungeon crawler RPG game in Unity 2D and I want to allow the player to go back and forth between rooms of the dungeon. This is the code for a Master script that sends the player to a re-entry point when the scene is loaded. This script becomes functional in scene 2.

public GameObject player;
public GameObject sceneEntry;

// Use this for initialization
void Start() 
{
    DontDestroyOnLoad(this.gameObject);
    SceneManager.sceneLoaded += OnSceneLoaded;
}

void OnSceneLoaded(Scene scene, LoadSceneMode mode)    
{
    Scene CurrentScene = SceneManager.GetActiveScene();
    string SceneName = CurrentScene.name;
    player = GameObject.Find("Player");
    sceneEntry = GameObject.Find("Re-entryPoint");

    if (SceneName == "Dungeon1") 
    {
        ReEnter();
    }

    if (SceneName == "Dungeon2") 
    {
        ReEnter();
    }
}

void ReEnter() 
{
    player.transform.position = new Vector2(sceneEntry.transform.position.x, sceneEntry.transform.position.y);
}

The problem is that when I want to enter scene 2 after going back to scene 1, I end up at the re-entry point in scene 2. I am not sure how to fix this.

\$\endgroup\$
  • \$\begingroup\$ It's not clear to me what the issue is. When you re-enter scene 2, you enter at the re-entry point in scene 2. That sounds...right? Remember that we don't know anything about your game or how it's supposed to work except what you tell us, so err on the side of explaining too much to make sure we get the right idea of what your game is supposed to do. \$\endgroup\$ – DMGregory Dec 3 '17 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.