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
0
Question by oz m · May 23, 2010 at 04:42 PM · crashplayerprefssavelocation

Playerprefs save player position

I am trying to use playerprefs to save the main character's position. I already was able to save an integer and then load it. I tried multiple ways using playerprefs to save a character's position, but apparently, those ways used too much data with playerprefs, because Unity crashed. Unity worked again after i removed the playerprefs from the scripts. Does anyone know how to save a players position without overloading unity?

Comment
Add comment
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

3 Replies

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

Answer by Eric5h5 · May 23, 2010 at 05:13 PM

Saving a few floats won't come anywhere near overloading Unity; something is wrong with the code you wrote. You save the position by writing the x, y, and z coords of the player using PlayerPrefs.SetFloat. If you want to make it a little simpler, you can use ArrayPrefs, which would let you write

PlayerPrefsX.SetVector3 ("PlayerPosition", transform.position);

Assuming transform is the transform of the player. Then you load it by doing this:

transform.position = PlayerPrefsX.GetVector3 ("PlayerPosition");
Comment
Add comment · Show 3 · 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 oz m · May 23, 2010 at 05:31 PM 0
Share

I did that and it overloaded

avatar image Eric5h5 · May 23, 2010 at 05:38 PM 0
Share

@oz m: It's not "overloading", it's crashing because you're doing something wrong. Post your code, because it's not really possible to say what the problem is without that.

avatar image asasDda · Sep 03, 2014 at 02:27 PM 0
Share

man thaaaaaaaaaaaaaaank you you really help me thank you so mach

avatar image
0

Answer by bernardfrancois · Mar 11, 2011 at 10:19 AM

On some platforms (especially Android and iOS) it can be really slow to save a lot of data in PlayerPrefs. Sometimes it can take multiple seconds, giving the impression that Unity crashed.

This could be the case if you're saving a lot more than just the player position.

A solution is to write a custom PlayerPrefs class where the data is only saved periodically to a file.

We've shared our own PlayerPrefs class for anyone to download: http://www.previewlabs.com/writing-playerprefs-fast/

Comment
Add comment · 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
0

Answer by babaji1234 · May 02, 2013 at 11:22 AM

here is a working script

 #pragma strict
 var player : Transform;
 var x : float;
 var y : float;
 var z : float;
 function Start () {
 
  if(PlayerPrefs.HasKey("x") && PlayerPrefs.HasKey("y") && PlayerPrefs.HasKey("z")) {
  
  x = PlayerPrefs.GetFloat("x");
  y = PlayerPrefs.GetFloat("y");
  z = PlayerPrefs.GetFloat("z");
  
  player.position.x = x;
  player.position.y = y;
  player.position.z = z;
  
  }
 }
 
 function Update () {
 
   x = player.position.x;
   PlayerPrefs.SetFloat("x", x);
   
   y = player.position.y;
   PlayerPrefs.SetFloat("y", y);
   
   z = player.position.z;
   PlayerPrefs.SetFloat("z", z);
   
 }
Comment
Add comment · Show 2 · 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 Jonathon82931 · Jul 13, 2013 at 03:45 PM 0
Share

anyone know of a java to c# converter? I think I would like to view the functions for this in c#

avatar image Ilgiz · Nov 13, 2014 at 09:23 AM 0
Share

i think, to use PlayerPrefs.SetFloat in Update function isn't a good idea...

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Player Prefs reset after crash? 1 Answer

high scores scene 2 Answers

"If there is no save data" or "If there are no PlayerPrefs" 2 Answers

PlayerPrefs and own Class 1 Answer

PlayerPrefs save after quit? 2 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