- Home /
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();
}
}
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?
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.
This is partially true. PlayerPrefs.Save() will save all modified prefs to the disk, But PlayerPrefs.Set(int,float,key) will also write them .
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.
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.
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.
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
Follow this Question
Related Questions
container class save based on Cherno's example 1 Answer
Save/load playerprefs 2 Answers
Problems with saving/loading score with PlayerPrefs [C#] 1 Answer
[Solved] PlayerPrefs Vector 3 Defaulting to 0,0,0 1 Answer
How i can PlayerPrefs List<>? 0 Answers