How to choose coordinates on Application.loadlevel

I am currently working on a game where the user can enter a building by walking up to the building and pressing the E key. When the player walks up to the building, it says “Press E to enter (building name)”, in this case the warehouse. That part works fine, the player easily loads into the warehouse when they stay on the trigger and press the E key.

When the player is in the warehouse, they can walk up to the warehouse door (Trigger) and press E to exit, and it leads them to my island scene. Now to my question. I am wondering when the player presses E to exit the warehouse, how can I determine where they spawn? I want the player to load outside of the warehouse, the same place they pressed the E key to enter the warehouse, so just outside the entrance to the warehouse. This may be a simple question, but I have no idea how to go about doing this.

I’ve looked at other topics similar to this, but couldn’t figure out how to tie it in with my question.

Bear with me, I have only been in Unity for about a week now, and have started writing scrips a few days ago, so I am very new to scripting, and this is my first post on UnityAnswers.

LoadWarehouseInterior.js (This is on the island scene, but this brings them inside the warehouse):

#pragma strict

var hud : boolean = false;
var checkKeyDown : boolean = false;


function OnGUI(){
	if(hud == true){
		GUI.Box(Rect(325,320,200,40), "Press E to enter Warehouse.");
		
	if(Input.GetKeyDown("e")){
			Application.LoadLevel("WarehouseInterior");
		}
	}
}

function OnTriggerStay () {	
	hud = true;
	
}

function OnTriggerExit (){
	hud = false;
}

LoadWarehouseExterior.js (This should take them to my island, in the specified location):

    #pragma strict
    
    var hud : boolean = false;
    var checkKeyDown : boolean = false;
    
    
    
    function OnGUI(){
    	if(hud == true){
    		GUI.Box(Rect(325,320,200,40), "Press E to exit the Warehouse.");
    		
    	if(Input.GetKeyDown("e")){
    			Application.LoadLevel("");
    		}
    	}
    }
    
    function OnTriggerStay () {	
    	hud = true;
    	
    }
    
    function OnTriggerExit (){
    	hud = false;
    }

If it helps, I would like the player to spawn at the following coordinates - X: 2731, Y: 121, Z: 2071

Since I will have numerous buildings the player can enter, I would appreciate it if the player spawns to the certain specified spot, since each exited building should have its own spawn point.

So how would I go about doing this? Like I said, I am new to unity and scripting, so please be easy with me in your answers, and help is appreciated!

Well for things like this it is a great idea to have a class that can control and manage the whole game, in this case the room transactions. You could create a class script that can be accessed globally, and when you load every level there should be a script in that game that checks what door the player came from and move the player acording.

Something like this:

static class GameController {

    var doorID : int;


    function EnterDoor(thisDoor:int){
        //Set you doorID here and load your new scene
        doorID = thisDoor;

        //To do this you would just write this in any script
        //     GameController.EnterDoor(1); when the player presses E on door 1
    }

}

Then when you have loaded a scene check what the door id was and move the player:

function Start(){
    if (GameController.doorID == 1){
        //Move position according
    }
}

Hope that helps, if you need any more help understanding just let me know.

Finally I got it working. For anyone else that is wondering a similar thing, here is my script I used:

LoadWarehouseInterior.js

#pragma strict

var hud : boolean = false;
var checkKeyDown : boolean = false;
static var doorID : int;

function OnGUI(){
	if(hud == true){
		GUI.Box(Rect(325,320,200,40), "Press E to enter Warehouse.");
		
	if(Input.GetKeyDown("e")){
			doorID = 1;
			Application.LoadLevel("WarehouseInterior");
		}
	}
}

function OnTriggerStay () {	
	hud = true;
	
}

function OnTriggerExit (){
	hud = false;
}

When the user enters my cube collider on the main island, they can press the E key to enter the door, then it sets the id of the door to one, then loads the warehouse.

LoadWarehouseExterior.js

#pragma strict

var hud : boolean = false;
var checkKeyDown : boolean = false;
static var doorIDLoad : int;


function OnGUI(){
	if(hud == true){
		GUI.Box(Rect(325,320,200,40), "Press E to exit Warehouse.");
		
	if(Input.GetKeyDown("e")){
			Application.LoadLevel("Island");
		}
	}
}

function OnTriggerStay () {	
	hud = true;
	
}

function OnTriggerExit (){
	hud = false;
}

Basically, all the above script does is to check if the player is on the collider, if so and they press the E key, they will exit to the Island scene.

FindDoorLocation.js

#pragma strict

function Awake() {
	if(LoadWarehouseInterior.doorID == 1){
		var player = GameObject.Find("First Person Controller");
        player.transform.position = Vector3(2731,125,2071);
    }
}

All the script above does is to verify that the doorID is equal to 1, and if it is (which it is) then it will move the player to the coordinates I specified, before showing the player the loaded scene. If you choose to use this script, of course you will have to change the doorID if you have multiple doors in your scene.