Help with loading player position after scene change

Hey everyone, ive been having trouble loading player positions between scenes. What i have now is an overworld with buildings you can enter, each being a seperate sene, with a trigger that returns you to the over world. What I would like to happen is that when the overworld scene loads, the player is place outside of the building they just exited, however the player keeps being placed at the starting point for the overworld scene. Heres what i have now.

this script is placed on the trigger that sends them to the building interior

sing UnityEngine;
using System.Collections;

public class NewLevelChange : MonoBehaviour {
public GameObject player;
//public LevelManager levelManager;

void Start () {
	player = GameObject.Find("Player");
	
}

void Update () {

}

void OnTriggerEnter(Collider col){
	
	if(col.gameObject.tag == "Player"){
		
		PlayerPrefs.SetFloat("PlayerX",player.transform.position.x);
		PlayerPrefs.SetFloat("PlayerY", player.transform.position.y);
		PlayerPrefs.SetFloat("PlayerZ", player.transform.position.z);
		
		Application.LoadLevel("Level2");
		
	}
}

}

and this is the script that should return them to the correct place in the overworld.

public var player : GameObject;
//var newPosition = Vector3(0,0,0);

function Start () {
player = GameObject.Find(“Player”);
}

function Update () {

}

function OnTriggerEnter(col : Collider){
Application.LoadLevel(“City”);
var newX :float = PlayerPrefs.GetFloat(“PlayerX”);
var newY :float = PlayerPrefs.GetFloat(“PlayerY”);
var newZ :float = PlayerPrefs.GetFloat(“PlayerZ”);

	Application.LoadLevel("City");
	
	player.transform.position = Vector3(newX,newY,newZ);

}

I’m rather new to Unity scripting so it’s probably some simple mistake, but any help or advice would be greatly appreciated :slight_smile:

as soon as you reach Application.LoadLevel, the next level will load, so the script will not continue on to the next line. You need a script in the city level with a Start function that sets the position:

function Start()
{
var newX :float = PlayerPrefs.GetFloat("PlayerX");
var newY :float = PlayerPrefs.GetFloat("PlayerY"); 
var newZ :float = PlayerPrefs.GetFloat("PlayerZ");
player.transform.position = Vector3(newX,newY,newZ);
}

also, you don’t really need PlayerPrefs for this, that’s more for between game sessions. simply create a static var in your script for this:

static var playerPosition : Vector3;

then you can access it by the name of the script from anywhere:

ScriptName.playerPosition = player.transform.position; (or vice versa)

if you like, you can still set the player prefs every time, of course.

hope this helps

That actually did work, however when i start the game the player position becomes the default of the static Vector 3 (0,0,0). Is there anyway to set the vector 3 to a specific position when the scrip starts, because I’m storing the value of the Vector 3 in a LevelManager that is created when the game starts and has Don’tDestroyOnLoad attached to it

as long as you have the data stored properly, you can retrieve it. If the var is static, it shouldn’t be reset on load anyway, so I’m guessing you never got the pos stored in it…

in the first script:

void OnTriggerEnter(Collider col){

    if(col.gameObject.tag == "Player"){

       PlayerPrefs.SetFloat("PlayerX",player.transform.position.x);
       PlayerPrefs.SetFloat("PlayerY", player.transform.position.y);
       PlayerPrefs.SetFloat("PlayerZ", player.transform.position.z);
       playerPosition = player.transform.position;

       Application.LoadLevel("Level2");

    }
} 

if you prefer to store it elsewhere, just replace playerPosition with wherever it’s at. Keep in mind having a var static will keep it intact between scene changes, just like DontDestroyOnLoad. DontDestroyOnLoad is useful for more complex entities (like the player itself).