- Home /
How to load a scene with playerprefs from a ui element
Hello everyone,
I am trying to figure out how to load a scene that has playerprefs saving the players position. I have a ui element that is on the main menu that the player clicks on - I just don't know how to tell it to load the playerprefs.
any help is welcomed
thank you
Answer by sean244 · Apr 14, 2018 at 07:35 PM
Ok, now I understand. This video should show exactly what you're trying to do https://youtu.be/QWhNrfc4OFE
This is the code. Just make the Load button call the Load function in this script.
using UnityEngine;
public class Player : MonoBehaviour
{
private Rigidbody2D _rb;
public void Load()
{
float pX;
float pY;
float pZ;
if (PlayerPrefs.GetInt("Saved") == 1)
{
pX = PlayerPrefs.GetFloat("p_x");
pY = PlayerPrefs.GetFloat("p_y");
pZ = PlayerPrefs.GetFloat("p_z");
transform.position = new Vector3(pX, pY, pZ);
}
}
public void Save()
{
PlayerPrefs.SetFloat("p_x", transform.position.x);
PlayerPrefs.SetFloat("p_y", transform.position.y);
PlayerPrefs.SetFloat("p_z", transform.position.z);
PlayerPrefs.SetInt("Saved", 1);
PlayerPrefs.Save();
Debug.Log("Saved");
}
void Start()
{
_rb = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
float speed = 3f;
float horizontal = Input.GetAxis("Horizontal");
_rb.velocity = new Vector2(horizontal * speed, _rb.velocity.y);
}
}
Thank you @sean244
ok, that is great, but the issue is i have the script attached to a trigger. And the script i need would be attached to a completely different scene referring to the scene with the player.
So here is the script I have with the trigger to save the players position.
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();
}
}
AND here is the script I have to try and load the player position that was saved with the previous script from a different scene. (the load script is NOT in the same scene)
using System.Collections;
using UnityEngine.Scene$$anonymous$$anagement;
using UnityEngine;
public class LoadSaved : $$anonymous$$onoBehaviour
{
public void Load()
{
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
Scene$$anonymous$$anager.LoadScene(PlayerPrefs.GetInt("Saved") == 1("sceneName"))
}
}
}
So, this is not working. It is not loading the saved player position from the other scene. What do I do?
Also I am sorry this has taken so much of your time @sean244 - but I really appreciate your time.
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
Your answer
Follow this Question
Related Questions
PlayerPrefs issue 2 Answers
Distribute terrain in zones 3 Answers
shop script not working 1 Answer