Hello all, I am trying to script out a way to load a game using PlayerPrefs, but I ran into an issue. After my level loads, my script doesn’t load the player position. I assume this is because the script is coming from another scene and is no longer active afterwards. Are there any suggestions on how I could go about fixing this?
This is my saving function.
function Awake ()
{
// Saves the character's X, Y, Z coordinates, coins and the current level
PlayerPrefs.SetFloat("PlayerX", GameObject.Find("Player Cube").transform.position.x);
PlayerPrefs.SetFloat("PlayerY", GameObject.Find("Player Cube").transform.position.y);
PlayerPrefs.SetFloat("PlayerZ", GameObject.Find("Player Cube").transform.position.z);
PlayerPrefs.SetInt("PlayerCoins", GameObject.Find("Player Cube").GetComponent( "CharacterMovement" ).Coins);
PlayerPrefs.SetString("CurrentLevel", Application.loadedLevelName);
}
And This is my loading function
function Update ()
{
// Resets the time to unpause the game
Time.timeScale = 1.0;
// Loads the saved Level
Application.LoadLevel( PlayerPrefs.GetString( "CurrentLevel" ) );
// Loads the Position for the player
GameObject.Find( "Player Cube" ).transform.position = Vector3( PlayerPrefs.GetFloat("PlayerX"), PlayerPrefs.GetFloat("PlayerY"), PlayerPrefs.GetFloat("PlayerZ") );
// Load the character's money
Coins = PlayerPrefs.GetInt( "PlayerCoins" );
}