title: Unity: How to switch scenes and when returning to the original scene, return player to the place they switched their scene
body: <p>I have two scenes: Main Street &amp; Building Scene
  When the player is Main Street, if the player's trigger box touches the building and the player presses &quot;q&quot;, the scene would switch to the Building Scene.
  I want it so that when the player exits the Building Scene and returns to the Main Street Scene, the player is back to the position they entered the Building Scene that they entered from. Apologies in advance if this doesn't make sense.</p>
  <p>sceneSwitchingScript:</p>
  <pre><code>    public int buildingToLoad;
      public Text InputText;
      public movement player;
      public Vector3 playerPrevPos;
  
      void OnTriggerEnter2D(Collider2D col){
          if(col.CompareTag(&quot;Player&quot;)){
              InputText.text = (&quot;[Q] to enter&quot;);
              if(Input.GetKeyDown(&quot;q&quot;)){
                  if (gameObject.tag == &quot;EntryPoint&quot;){
                      playerPrevPos = new Vector3(player.transform.position.x, player.transform.position.y, player.transform.position.z);
                  }
                  //Debug.Log(gameObject.tag);
                  Application.LoadLevel(buildingToLoad);  
              }
              
          }
      }
  
      void OnTriggerStay2D(Collider2D col){
          if(col.CompareTag(&quot;Player&quot;)){
              if(Input.GetKeyDown(&quot;q&quot;)){
                  //spawn = new Vector3(player.transform.position.x, player.transform.position.y, player.transform.position.z)
                  Application.LoadLevel(buildingToLoad);
                  if (gameObject.tag == &quot;EntryPoint&quot;){
                      playerPrevPos = new Vector3(player.transform.position.x, player.transform.position.y, player.transform.position.z);
                  }
              }
          }
      }
  
      void OnTriggerExit2D(Collider2D col){
          if(col.CompareTag(&quot;Player&quot;)){
              InputText.text = (&quot;&quot;);
          }
      }
  </code></pre>
  <p>Setting the player's position when they exit the building</p>
  <pre><code>public switchScene ss;
  void OnTriggerStay2D(Collider2D col){
          if(Input.GetKeyDown(&quot;q&quot;)){
              if(col.gameObject.CompareTag(&quot;ExitPoint&quot;)){
                  transform.position = ss.playerPrevPos;
              }       
          }
      }
  </code></pre>
  <p>However, these two scrips do not work and I'm not sure if this is related but when I make the player do the switch scene thing in-game, this error pops up:</p>
  <p><strong>NullReferenceException: Object reference not set to an instance of an object
  movement.OnTriggerStay2D (UnityEngine.Collider2D col)</strong></p>
  <p>This error message mentions the error on this line:</p>
  <pre><code>transform.position = ss.playerPrevPos;
  </code></pre>