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 kurotatsu · May 10, 2012 at 06:53 AM · positionplayerprefssaving

Player Prefs saving position Script assistance.

I've been working on this script for 3 days and am finally asking for assistance. I've managed to save Int's using PlayerPrefs, but can't get my positioning right. I've used answers found here and the script reference to put this together.

I'd like assistance rewriting this, and adding vector3 to save and load for my character.

Here's the parts of the script throwing errors:

//-----Players Position----------

var PlayerX : float;

var PlayerY : float;

var PlayerZ : float;

var PlayerPosition : Transform;

var player : GameObject;

function Update(){

//=======Setting Player position for save====

PlayerX =(PlayerPosition.transform.position.x);

PlayerY =(PlayerPosition.transform.position.y);

PlayerZ =(PlayerPosition.transform.position.z);

}

//saving playerPrefs

function saveAttributes() {

 PlayerPrefs.SetFloat("PlayerX");
 PlayerPrefs.SetFloat("PlayerY");
 PlayerPrefs.SetFloat("PlayerZ");

}

function loadstuff () {

 PlayerX = PlayerPrefs.GetFloat("PlayerX");
 PlayerY = PlayerPrefs.GetFloat("PlayerY");
 PlayerZ = PlayerPrefs.GetFloat("PlayerZ");

 PlayerPosition.transform.position.x = ("PlayerX");
 PlayerPosition.transform.position.y = ("PlayerY");
 PlayerPosition.transform.position.z = ("PlayerZ");
 player.transform.position = ("PlayerPosition");
 

}

Here are the errors:

BCE0017: The best overload for the method 'UnityEngine.PlayerPrefs.SetFloat(String, float)' is not compatible with the argument list '(String)'.

BCE0022: Cannot convert 'String' to 'float'.

I wouldn't mind converting this to use arrayPrefs, but I need help writing it versus a link to the script reference, or wiki. I've managed to learn from veiwing examples here on Answers this much through a diligent search and try method.

Any assistance would be most appreciated, thank you.

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

2 Replies

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

Answer by kurotatsu · May 11, 2012 at 03:42 AM

I got it to work. Many thanks for the assist. I'm going to post my updated version here for others to use as many of the answers to similar questions I find too vague.

 private var PlayerX : float;
 
 private var PlayerY : float;
 
 private var PlayerZ : float;
 
 var PlayerPosition : Transform;
 
 var player : GameObject;
 
 function Update(){
 
 //=======Setting Player position for save====
 
 PlayerX =(PlayerPosition.transform.position.x);
 
 PlayerY =(PlayerPosition.transform.position.y);
 
 PlayerZ =(PlayerPosition.transform.position.z);
 
 //=====Press "K" key to save position.================
 
 if (Input.GetKeyUp ("k")){
 
  saveAttributes();
  
  }
 
 //=====Press "L" key to load position.================
 
 if (Input.GetKeyUp ("l")){
 
  loadstuff();
  
  }
 
 
 }
 
 //saving playerPrefs
 
 function saveAttributes() {
 
 PlayerPrefs.SetFloat("PlayerX",PlayerPosition.transform.position.x);
  
 PlayerPrefs.SetFloat("PlayerY",PlayerPosition.transform.position.y);
  
 PlayerPrefs.SetFloat("PlayerZ",PlayerPosition.transform.position.z);
 
 }
 
 function loadstuff () {
 
 
 PlayerPosition.transform.position.x = (PlayerPrefs.GetFloat("PlayerX"));
 
 PlayerPosition.transform.position.y = (PlayerPrefs.GetFloat("PlayerY"));
 
 PlayerPosition.transform.position.z = (PlayerPrefs.GetFloat("PlayerZ"));
 
 }

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 syclamoth · May 11, 2012 at 03:56 AM 2
Share

A vague answer that tells you how to find the information you require is still more useful than a specific answer that only helps one person.

avatar image kurotatsu · May 11, 2012 at 09:58 PM 0
Share

I'm just saying, one might think to generalize the information to assist the many.

A person being linked repeatedly to the same link to the script reference 5 times for every answer they search before getting desperate enough to post the same question for the 50th time on the site doesn't help half as many as seeing a complete working example of what the script reference is getting at, so they can incorporate the information into their own script.

avatar image
2
Wiki

Answer by flamy · May 10, 2012 at 07:01 AM

 //=======Setting Player position for save====
 
 PlayerX =(PlayerPosition.transform.position.x); PlayerY =(PlayerPosition.transform.position.y); PlayerZ =(PlayerPosition.transform.position.z);
 
 } //saving playerPrefs function saveAttributes() {
 
 PlayerPrefs.SetFloat("PlayerX");
 PlayerPrefs.SetFloat("PlayerY");
 PlayerPrefs.SetFloat("PlayerZ");
 }

you are not saving the values here. SetFloat will take a string and a value. the one you have entered is a string that identifies the value to be followed. it should be like.

 {
    PlayerPrefs.SetFloat("PlayerX",PlayerX);
    PlayerPrefs.SetFloat("PlayerY",PlayerY);
    PlayerPrefs.SetFloat("PlayerZ",PlayerZ);
 }

also refer scripting refernce for this function

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

7 People are following this question.

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

Related Questions

PlayerPrefs Saving Player's Position Java Script 2 Answers

How to create a save and load game proccess like SCP: Containment Breach? 0 Answers

Save Values On Closing of Game 1 Answer

Unable to set Rotation 1 Answer

How to load a scene with playerprefs from a ui element 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