Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by bunnynsnake · Apr 16, 2018 at 04:32 PM · c#uisceneplayerprefsload

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

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TreyH · Apr 16, 2018 at 06:22 PM 1
Share

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.

avatar image bunnynsnake TreyH · Apr 16, 2018 at 07:16 PM 0
Share

@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?

avatar image bunnynsnake TreyH · Apr 16, 2018 at 08:50 PM 0
Share

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;
    }
  }
 
avatar image Runalotski · Apr 16, 2018 at 06:41 PM 1
Share

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.

avatar image bunnynsnake Runalotski · Apr 16, 2018 at 07:16 PM 0
Share

@Ranalotski thank you for giving me those options. I will consider rethinking the code.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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")); 


Comment
Add comment · Show 13 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bunnynsnake · Apr 16, 2018 at 09:20 PM 0
Share

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;
     }
   }
avatar image shadowpuppet bunnynsnake · Apr 16, 2018 at 09:38 PM 1
Share

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):
 }
 }
 }
avatar image bunnynsnake shadowpuppet · Apr 16, 2018 at 10:01 PM 0
Share

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?

Show more comments
avatar image bunnynsnake · Apr 17, 2018 at 01:32 AM 0
Share

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

avatar image shadowpuppet bunnynsnake · Apr 17, 2018 at 02:11 AM 1
Share

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

avatar image shadowpuppet shadowpuppet · Apr 17, 2018 at 01:04 PM 1
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

143 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges