Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by Kelly104 · Feb 01, 2012 at 09:13 PM · playerprefssavinglocation

Saving Location in Unity

I'm making a game in Unity, and I'm trying to use PlayerPrefs to save the player's location. I want a script that will save the player's position every few seconds and then when I close the game and reopen it, it will load the last place I was. Here is the code I have so far. Please help!

 //saving it
 var PlayerPosition = Vector3(0,0,0);
 var PlayerCurrentPosition: ; //constantly changes - time function
 var SavedPlayerPosition: ;
 
 function Update ()
 {
    
 PlayerPrefs.SetFloat("PlayerX", transform.position.x);
 PlayerPrefs.SetFloat("PlayerY", transform.position.y);
 PlayerPrefs.SetFloat("PlayerZ", transform.position.z);
 
 }
 
 //loading it back - when to load it - on load, game start
 function Update (loadPosition)
 {
 
 if () {
         
 Vector3 newPosition = Vector3 ("PlayerX", "PlayerY", "PlayerZ");
 newPosition.x = PlayerPrefs.GetFloat("PlayerX");
 newPosition.x = PlayerPrefs.GetFloat("PlayerY");
 newPosition.x = PlayerPrefs.GetFloat("PlayerZ");
 transform.position = newPosition;
 
 }
Comment
Add comment · Show 1
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 flamy · Feb 07, 2012 at 05:56 AM 0
Share

dude you are assigning x y and z everything to x in the newposition vector!! and also in OnApplicationQuit or somewhere else call PlayerPrefs.Save() (if ur unity is 3.4 are above!!)

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by syclamoth · Feb 01, 2012 at 09:32 PM

You're saving the position correctly, there's no problem there.

As for the second bit, well that's not so good.

To reload the positions, all you really need to do is this:

 // put it in Start, so it executes once per run:
 function Start()
 {
     var newXpos : float = PlayerPrefs.GetFloat("PlayerX");
     var newYpos : float = PlayerPrefs.GetFloat("PlayerY");
     var newZpos : float = PlayerPrefs.GetFloat("PlayerZ");
     transform.position = Vector3(newXpos, newYpos, newZpos);
 }
Comment
Add comment · Show 4 · 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 Kelly104 · Feb 06, 2012 at 04:05 PM 0
Share

Here's my new edited script:

var PlayerX : int; var PlayerY : int; var PlayerZ : int;

function Save () {

PlayerPrefs.SetFloat("PlayerX", transform.position.x); PlayerPrefs.SetFloat("PlayerY", transform.position.y); PlayerPrefs.SetFloat("PlayerZ", transform.position.z);

}

function Start () { var newXpos : float = PlayerPrefs.GetFloat("PlayerX"); var newYpos : float = PlayerPrefs.GetFloat("PlayerY"); var newZpos : float = PlayerPrefs.GetFloat("PlayerZ"); transform.position = Vector3(newXpos, newYpos, newZpos); }

All of the compiler errors went away, but now the character is either floating or the terrain is falling away (I never changed any script but this one.) Do you have any idea why?

avatar image syclamoth · Feb 06, 2012 at 11:58 PM 0
Share

Well, I can only assume that it's a completely unrelated issue. What are the three 'PlayerX' variables at the top for?

avatar image qwertyqaz · Feb 07, 2012 at 05:49 AM 0
Share

when I tryed this i got "Load operation failed. The requested operation cannot be performed on a file with a user-mapped sectionion open." in monodevelep

avatar image flamy · Feb 07, 2012 at 05:59 AM 0
Share

mostly your values didnt get saved!! so the value would be 0 at everything tht point would either be below or above terrain!!!

jus a guess

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

7 People are following this question.

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

Related Questions

A node in a childnode? 1 Answer

PlayerPrefs for unlocking items 1 Answer

PlayerPrefs.GetString not saving past values 0 Answers

Player Prefs saving position Script assistance. 2 Answers

saving highscores 1 Answer


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