- Home /
How to Load PlayerPrefs from a different scene
I am trying to load the saved players position from a UI on a different scene.
Here is the code I have that is attached to a trigger and should save the players exact position.
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class Tigger : MonoBehaviour
{
public GameObject Trigger;
public GameObject Animator;
public GameObject player;
public float PX;
public float PY;
public float PZ;
private void Start()
{
if (PlayerPrefs.HasKey("P_X") && PlayerPrefs.HasKey("P_Y") && PlayerPrefs.HasKey("P_Z"))
{
PX = PlayerPrefs.GetFloat("P_X");
PY = PlayerPrefs.GetFloat("P_Y");
PZ = PlayerPrefs.GetFloat("P_Z");
Vector3 posVec = new Vector3(PX, PY, PZ);
player.transform.position = posVec;
}
}
private void OnTriggerEnter()
{
Animator.GetComponent< Animation > ().Play("Music down");
}
private void OnTriggerExit()
{
Destroy(Trigger);
PlayerPrefs.SetFloat("P_X", player.transform.position.x);
PlayerPrefs.SetFloat("P_Y", player.transform.position.y);
PlayerPrefs.SetFloat("P_Z", player.transform.position.z);
PlayerPrefs.SetInt("Saved", 1);
PlayerPrefs.Save();
}
}
And here is the code that I have for loading the players saved position attached to a UI Component :
using System.Collections;
using UnityEngine.SceneManagement;
using UnityEngine;
public class LoadSaved : MonoBehaviour
{
public void Load()
{
if (Input.GetMouseButtonDown(0))
{
SceneManager.LoadScene(PlayerPrefs.GetInt("Saved") == 1("scene1"))
}
}
}
The issue is the script is not working to load the players saved position. What might be the issue?
Any help is welcomed.
Thank you
PlayerPrefs are scene independent. I think your problem is that weird syntax in your Load function:
Scene$$anonymous$$anager.LoadScene(PlayerPrefs.GetInt("Saved") == 1("scene1"))
This isn't correct syntax? There are several issues there and it isn't the purpose of this forum to correct basic C# syntax understandings. You need to load the scene itself first, then have something in that scene read / implement your position values.
@TreyH I thought that saving playerprefs to a named int would work. Thank you for letting me know that that makes no sense. So I would just have the main menu UI load the scene name and then when the scene is loaded I use on awake to load player position?
Should I load the players position on awake like this?
using System.Collections;
using UnityEngine.Scene$$anonymous$$anagement;
using UnityEngine;
public class LoadSaved : $$anonymous$$onoBehaviour
{
public void Awake ()
{
x = player.transform.position.x;
PlayerPrefs.GetFloat("P_X ", x);
y = player.transform.position.y;
PlayerPrefs.GetFloat("P_Y ", y);
z = player.transform.position.z;
PlayerPrefs.GetFloat("P_Z ", z);
Vector3 posVec = new Vector3(x,y,z);
player.transform.position = posVec;
}
}
Hi, there are a few ways you can do this here are two.
Tag your object with the data to not be destroyeds on load so it lives long enough in the next scene. Object.DontDestroyOnLoad.
You can also save the data to PlayerPrefs to retrieve later, it does save as plain text, (Don't us it for passwords) and can be used sparingly to hold simple data between game sessions. PlayerPrefs.
I'm not sure the best way to do this so I have posted these options to look at while others respond.
@Ranalotski thank you for giving me those options. I will consider rethinking the code.
Answer by shadowpuppet · Apr 16, 2018 at 09:01 PM
Don't see where you are loading the actual player position from the floats you saved.
playerPosition.transform.position.x = (PlayerPrefs.GetFloat("P_X"));
playerPosition.transform.position.y = (PlayerPrefs.GetFloat("P_Y"));
playerPosition.transform.position.z = (PlayerPrefs.GetFloat("P_Z"));
I think my issue us that I don't know what to call to load the position. I have been looking online for this for 4 days...this apparently isn't easy to find. I now know it has to be called in the same scene after its loaded. So would I do this on awake??
using System.Collections;
using UnityEngine.Scene$$anonymous$$anagement;
using UnityEngine;
public class LoadSaved : $$anonymous$$onoBehaviour
{
public void Awake ()
{
x = player.transform.position.x;
PlayerPrefs.GetFloat("P_X ", x);
y = player.transform.position.y;
PlayerPrefs.GetFloat("P_Y ", y);
z = player.transform.position.z;
PlayerPrefs.GetFloat("P_Z ", z);
Vector3 posVec = new Vector3(x,y,z);
player.transform.position = posVec;
}
}
Not sure, Awake may be too soon. Start maybe? If those don't work.... In the new scene you must have the player there already since you want to load his position ( and nothing about having to load him as a prefab or anything). So if he is in the new scene already he is at a set location when the scene starts ( just not where you want him) so he could be in a triggerzone that has that script on it
void OnTriggerStay(Collider other) {
if(other.tag == "Player"){
//put the loading position stuff here.then you can just destroy this gameObject since you don't need it anymore
Destroy (gameObject):
}
}
}
O$$anonymous$$, so i would need to know the exact position they are in at all times? The issue with that is I have multiple save positions in the same scene, so how would I load them to a position that is unknown to me? I mean i know the save positions...but i will not know which one the player chose to save at. will that script work for that?
This worked. I attached this script to a gameobject
using UnityEngine;
using System.Collections;
using UnityEngine.Scene$$anonymous$$anagement;
public class Tigger : $$anonymous$$onoBehaviour
{
public GameObject Trigger;
public GameObject Animator;
public GameObject player;
public float PX;
public float PY;
public float PZ;
private void Start()
{
if (PlayerPrefs.Has$$anonymous$$ey("P_X") && PlayerPrefs.Has$$anonymous$$ey("P_Y") && PlayerPrefs.Has$$anonymous$$ey("P_Z"))
{
PX = PlayerPrefs.GetFloat("P_X");
PY = PlayerPrefs.GetFloat("P_Y");
PZ = PlayerPrefs.GetFloat("P_Z");
Vector3 posVec = new Vector3(PX, PY, PZ);
player.transform.position = posVec;
}
}
private void OnTriggerEnter()
{
Animator.GetComponent<Animation>().Play("$$anonymous$$usic down");
}
private void OnTriggerExit()
{
Destroy(Trigger);
PlayerPrefs.SetFloat("P_X", player.transform.position.x);
PlayerPrefs.SetFloat("P_Y", player.transform.position.y);
PlayerPrefs.SetFloat("P_Z", player.transform.position.z);
PlayerPrefs.SetInt("Saved", 1);
PlayerPrefs.Save();
}
}
Than this script to the player
using System.Collections;
using UnityEngine.Scene$$anonymous$$anagement;
using UnityEngine;
public class LoadSaved : $$anonymous$$onoBehaviour
{
public GameObject player;
float x;
float y;
float z;
void OnTriggerStay(Collider other)
{
if (other.tag == "Player")
{
Scene$$anonymous$$anager.LoadScene(""); // enter the scene name here
x = player.transform.position.x;
PlayerPrefs.GetFloat("P_X ", x);
y = player.transform.position.y;
PlayerPrefs.GetFloat("P_Y ", y);
z = player.transform.position.z;
PlayerPrefs.GetFloat("P_Z ", z);
Vector3 posVec = new Vector3(x, y, z);
player.transform.position = posVec;
Destroy(gameObject);
}
}
}
and this script to the main menu UI
using System.Collections;
using UnityEngine.Scene$$anonymous$$anagement;
using UnityEngine;
public class loadlevel : $$anonymous$$onoBehaviour
{
public void LoadScene()
{
Scene$$anonymous$$anager.LoadScene(""); //enter scene name here
}
}
Thanks for leading me in the right spot
Great glad it worked. That is all that matters so ignore my next questions then...but I was just assu$$anonymous$$g that the first script (Tigger) was on the gameObject with a collider that was a trigger because it says to do something when the player exits. And the second script (loadsaved) also on a gameobject with a collider that is a trigger because it does something when the player enters. long as it works. I have some scripts that I have no clue why they work but they do so I leave well enough alone.LOL
you know I think I solved my own problem as well ( having to hit the load button twice) by taking my own advice and put the player start position of the scene in a trigger zone that will then load his position after the scene has loaded. Just in my load funtion set a bool loadingScene = true, then in the trigger zone in that scene say if(loading== true) thn the load position code. The bool will prevent me from loading his position if I am just opening the scene to test it and want to start at the beginnning.
Your answer
Follow this Question
Related Questions
Slider won't slide, issue assigning PlayerPrefs and then changing the PlayerPrefs' value 1 Answer
My scene won't load! 1 Answer
How to make a Saved/Favorites system in unity? 0 Answers
Multiple Instances of My Current Scene? 0 Answers
How to get a dropdown menu to change the colour of a reticule on another scene 0 Answers