Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 SITH-RULER · Nov 04, 2016 at 03:56 PM · vector3quaternionplayerprefssaveload

[Solved] PlayerPrefs Vector 3 Defaulting to 0,0,0

I Wrote A simple code to save position/rotation of the player but i ran into a problem the players position after erasing playerprefs (Have a special button in the menu that deletes playerprefs from the registry) the players position is being set to 0,0,0 and thus starts falling to nothingness. Any ideas on what i did wrong? Thanks!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SaveGame : MonoBehaviour 
 {
     [Tooltip("Assign The PlayerObject To Save Its Position")]
     public GameObject Player;//Assign Position.
     [Tooltip("Assign The PlayerObject To Save Its Rotation")]
     public GameObject RPlayer;//Assign Rotatio.
 
      void Start()
     {
         float Xpos = PlayerPrefs.GetFloat ("PlayerX");
         float Ypos = PlayerPrefs.GetFloat ("PlayerY");
         float Zpos = PlayerPrefs.GetFloat ("PlayerZ");
         Player.transform.position = new Vector3 (Xpos, Ypos, Zpos);
 
         float Xrot = PlayerPrefs.GetFloat ("RPlayerX");
         float Yrot = PlayerPrefs.GetFloat ("RPlayerY");
         float Zrot = PlayerPrefs.GetFloat ("RPlayerZ");
         float Wrot = PlayerPrefs.GetFloat ("RPlayerW");
         Player.transform.rotation = new Quaternion (Xrot, Yrot, Zrot, Wrot);
                                                 
     }
         
     void Update()
     {
         
         PlayerPrefs.SetFloat ("PlayerX", Player.transform.position.x); // Saves X position.
         PlayerPrefs.SetFloat ("PlayerY", Player.transform.position.y);// Saves y position.
         PlayerPrefs.SetFloat ("PlayerZ", Player.transform.position.z);// Saves z position.
 
         PlayerPrefs.SetFloat ("RPlayerX", RPlayer.transform.rotation.x); // Saves X Rotation.
         PlayerPrefs.SetFloat ("RPlayerY", RPlayer.transform.rotation.y); // Saves y Rotation.
         PlayerPrefs.SetFloat ("RPlayerZ", RPlayer.transform.rotation.z); // Saves z Rotation.
         PlayerPrefs.SetFloat ("RPlayerW", RPlayer.transform.rotation.w); // Saves w Rotation.
 
     }
         
 }
 
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 Landern · Nov 04, 2016 at 05:37 PM 0
Share

If you use GetFloat(look at the documentation and the default values for the second parameter) and there isn't a corresponding key and the default value isn't set it will result in default(float) which is 0.0f. So assu$$anonymous$$g you Set the values, stop the game, start, notice it loads the values you set... you're super happy then use the $$anonymous$$enu item you mention to delete the keys... start/play the game again it will result in all the values being set to zero(0) due to the default value not being set on the GetFloat call.

1 Reply

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

Answer by SITH-RULER · Nov 09, 2016 at 06:34 PM

Figured Out the problem just added a reference to player.transform.position in the update function itself

             X = Player.transform.position.x;
         Y = Player.transform.position.y;
         Z = Player.transform.position.z;
 
         PlayerPrefs.SetFloat ("X", X);
         PlayerPrefs.SetFloat ("Y", Y);
         PlayerPrefs.SetFloat ("Z", Z);

and then checked if there were any values in the Corresponding keys by using

 if(PlayerPrefs.HasKey("X") && PlayerPrefs.HasKey("Y") && PlayerPrefs.HasKey("Z))
 {
 // Code to load the position goes here. 
 }
 

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

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

How can I continue the game without losing values after Game Over? 1 Answer

Saving and loading game with PlayerPrefs 1 Answer

Playerprefs doesn't save anything. 2 Answers

save string from array to player prefs 1 Answer

save unlocked character by Playerprefs 3 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