- Home /
Why doesn't my character teleport to saved location when loading?
I am implementing a load game method on my player script, I realize that this should probably be in a game manager script but oh well...
The only data that gets stored is a boolean value, an integer that represents which spawn point they should spawn at, the position of that spawn point, and the players inventory. I know this data is being saved and loaded because the inventory is re-populated when I start the game, and the output of the below debug calls is (Also I left out the code for the inventory population as not to clutter up the problem code):
LOADING GAME:
Save Spot: 2
Teleporting to: 9.816902 -0.4699752 22.40802 From:
Current Position: 19.99548 1.585351 -20.008
New Position: 9.816902 -0.4699752 22.40802
As you can see, the 'current position' and 'new position' are the exact same line of code, but after assigning the transform of the player to the new spot, somehow the transform is updated but the character is not. any ideas?
private void LoadGame(PlayerData tempPlayer)
{
if (tempPlayer != null)
{
Debug.Log("LOADING GAME:");
// Assigning variables
saveSpot = tempPlayer.saveSpot;
level2 = tempPlayer.level;
string[] inventory = tempPlayer.inventory;
savePos = tempPlayer.position;
Debug.Log("Save Spot: " + saveSpot);
// Move player to save position
Vector3 position = new Vector3(tempPlayer.position[0], tempPlayer.position[1], tempPlayer.position[2]);
Debug.Log("Teleporting to: " + position.x + " " + position.y + " " + position.z + " From: ");
Debug.Log("Current Position: " + transform.position.x + " " + transform.position.y + " " + transform.position.z);
gameObject.transform.position = position;
Debug.Log("New Position: " + transform.position.x + " " + transform.position.y + " " + transform.position.z);
}
Your problem is likely elsewhere, turn off other scripts and see if it helps. Check that your loading script is actually on the player, and not on another gameobject.
When does your loading happen? Possible that another script is assigning a cached value to the position. Also if your loading happens before start() check if any script related to the player is defaulting the transform in start(). If thats the case you have to also update the cached / default values when restoring the position state.
Are you trying to save the Vector3
as is? if so they aren't serializable
. See these answers for a solution if so.
Answer by SeaPeaMe · Dec 03, 2019 at 04:15 AM
You might've accidentally forgot to get the saved position XD
private void LoadGame(PlayerData tempPlayer)
{
if (tempPlayer != null)
{
Debug.Log("LOADING GAME:");
// Assigning variables
saveSpot = tempPlayer.saveSpot;
level2 = tempPlayer.level;
string[] inventory = tempPlayer.inventory;
savePos = tempPlayer.position;
Debug.Log("Save Spot: " + saveSpot);
// Move player to save position
Vector3 position = new Vector3(saveSpot.x, saveSpot.y, saveSpot.z); // The error might've been here, but I don't know if it is a Vector3 or an array
Debug.Log("Teleporting to: " + position.x + " " + position.y + " " + position.z + " From: ");
Debug.Log("Current Position: " + transform.position.x + " " + transform.position.y + " " + transform.position.z);
gameObject.transform.position = position;
print("Teleport Complete!"); // You can also use print instead of Debug.Log, just to make it faster to type :)
}
Unfortunately that's not the error :( I did however switch it to Vector3 position = new Vector3(tempPlayer.position[0], tempPlayer.position[1], tempPlayer.position[2]);
but that's not working either... No matter what I do I just cannot get the player to teleport.
$$anonymous$$aybe disable the player's rigidbody and script (If the code you've posted isn't in the same one) and then try move the player, that was the problem for me once upon a time
Your answer
Follow this Question
Related Questions
Rotation question. 1 Answer
Player Prefs Dosen't work on android 0 Answers
get variable 2 Answers
Mouse Look Script 1 Answer
Script Crashing (Only on first run) 2 Answers