• 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 $$anonymous$$ · Feb 02, 2018 at 08:01 PM · 3dplayerprefssaveloadsaveload

Playerprefs doesn't save anything.

I'm trying to make a 30x30 block area in which every block has a script that saves it's values according to the position to the block. But for some reason the playerprefs doesn't save the values. Here's the script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class scri : MonoBehaviour {
 
     public Mesh[] meshes;
 
     public MeshFilter meshf;
 
     public int loaded;
 
     public int meshNumber;
 
     public string position;
 
     public string hasLoadedposition;
 
     void Start () {
 
         position = transform.position.x + ":" + transform.position.y;
         hasLoadedposition = position + "hasLoaded";
 
 
         PlayerPrefs.GetInt(hasLoadedposition, loaded);
 
         if(loaded == 1)
         {
             Debug.Log("saved and loaded correctly");
         } else
         {
             Debug.Log("loading for first time or error");
             PlayerPrefs.SetInt(hasLoadedposition, 1);
         }
 
         meshf = GetComponent<MeshFilter>();
 
         meshNumber = Random.Range(0, meshes.Length);
 
 
         meshf.mesh = meshes[meshNumber];
 
     }
 
     public void DeleteAll()
     {
         PlayerPrefs.DeleteAll();
     }
 
 }
 
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 TreyH · Feb 02, 2018 at 10:06 PM 0
Share

You might want to revise how you're doing this. Positions in Unity are stored as Vector3 instances, with each component being a 32-bit floating point number. Those are probably not going to give you reliable, repeatable values to be used as keys for storing / retrieving PlayerPrefs key-value-pairs. Have you tried printing out that string to make sure it's the same across multiple playthroughs?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Bonfire-Boy · Feb 02, 2018 at 08:06 PM

You have to call PlayerPrefs.Save() to actually save them to disk. Otherwise you're just changing the runtime copy.

This means that you can choose when to save things. It's handy if, for example, you only want a player's progress to be saved on completion of a level.

https://docs.unity3d.com/ScriptReference/PlayerPrefs.html

Comment
Add comment · Show 5 · 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 $$anonymous$$ · Feb 02, 2018 at 08:54 PM 0
Share

It still doesn't work.

avatar image melsy · Feb 02, 2018 at 09:18 PM 0
Share

This is partially true. PlayerPrefs.Save() will save all modified prefs to the disk, But PlayerPrefs.Set(int,float,key) will also write them .

avatar image Bonfire-Boy melsy · Feb 02, 2018 at 09:38 PM 0
Share

Is that an undocumented function? I've not come across it.

It's certainly true that the Getters retrieve from file, rather than the local copy, which reduces the usefulness of delayed saving. In most cases, if you want anything other than saving things when they're set, then you need your own local copy anyway.

I don't usually use PlayerPrefs, for these kind of reasons, but when I do I like to have a generic wrapper class so I can choose exactly what I'm saving and loading, and when.

avatar image melsy Bonfire-Boy · Feb 02, 2018 at 09:43 PM 0
Share

Yeah when i used player prefs in the past Set is how i wrote them I didnt even use Save fo most of the time. .It probably shouldnt work but it does. I built a few mobile games that way before i got unlazy and figured out serialization.

Show more comments
avatar image
0

Answer by melsy · Feb 02, 2018 at 09:05 PM

At first glance it looks like you are setting the int asLoadedaposition string different depending on where the object is. So your calling a player pref that doesn't exist.

Comment
Add comment · Show 1 · 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 Bonfire-Boy · Feb 02, 2018 at 09:10 PM 0
Share

Yeah definitely a good idea to log what key string you're using each time. Or simplify things by using a const key string to see if that makes a difference.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

85 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

Related Questions

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

Saving and loading game with PlayerPrefs 1 Answer

save string from array to player prefs 1 Answer

Save Time Button Settings in Player Prefs 2 Answers

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