PlayerPrefs not debugging
I have some string PlayerPrefs which I am checking in Awake as:
if (!PlayerPrefs.HasKey){PlayerPref.SetString("Price1" + name + parent.name, price1)}
if (!PlayerPrefs.HasKey){PlayerPref.SetString("Price2" + name + parent.name, price2)}
if (!PlayerPrefs.HasKey){PlayerPref.SetString("Price3" + name + parent.name, price3)}
The script is attached to 3 different gameObjects. The script is enabled and the gameObjects are active when the play button is pressed in the editor. But when I debug these in awake()
or in start()
only one gameObjects values are printed. Others debug nothing. I am confused why?
can you copy the full code? since those lines would not even compile and you are not checking anything just setting a new value. But maybe what happens is that the playerpref has a key but the string in that key is empty, but would need extra details
I'm sorry I rechecked the question and in the question I am checking if the PlayerPrefs.Has$$anonymous$$ey returns true whereas in my script I'm checking if it returns false; @xxmariofer
but where are you debugging, did you try what i said? checking if its an empty string? you can use the playerpref destroyall for errasing all keys so all keys get set again
Answer by ProNoob2 · Jul 29, 2019 at 10:37 AM
[SerializeField] private ulong Food1Price;
[SerializeField] private ulong Food2Price;
[SerializeField] private ulong Food3Price;
[SerializeField] private ulong Food4Price;
private void Awake()
{
PlayerPrefs.DeleteAll();
stage = transform.parent.gameObject;
if (!PlayerPrefs.HasKey("Food1Price" + NumFromString(name) + "_" + NumFromString(stage.name)))
{
//Debug.Log("Price1" + NumFromString(name) + "_" + NumFromString(stage.name) + ": " + Food1Price);
PlayerPrefs.SetString("Food1Price" + NumFromString(name) + "_" + NumFromString(stage.name), Food1Price.ToString());
}
else
{
Food1Price = ulong.Parse(PlayerPrefs.GetString("Food1Price" + NumFromString(name) + "_" + NumFromString(stage.name)));
}
if (!PlayerPrefs.HasKey("Food2Price" + NumFromString(name) + "_" + NumFromString(stage.name)))
{
//Debug.Log("Price2" + NumFromString(name) + "_" + NumFromString(stage.name) + ": " + Food2Price);
PlayerPrefs.SetString("Food2Price" + NumFromString(name) + "_" + NumFromString(stage.name), Food2Price.ToString());
}
else
{
Food2Price = ulong.Parse(PlayerPrefs.GetString("Food2Price" + NumFromString(name) + "_" + NumFromString(stage.name)));
}
if (!PlayerPrefs.HasKey("Food3Price" + NumFromString(name) + "_" + NumFromString(stage.name)))
{
//Debug.Log("Price3" + NumFromString(name) + "_" + NumFromString(stage.name) + ": " + Food3Price);
PlayerPrefs.SetString("Food3Price" + NumFromString(name) + "_" + NumFromString(stage.name), Food3Price.ToString());
}
else
{
Food3Price = ulong.Parse(PlayerPrefs.GetString("Food3Price" + NumFromString(name) + "_" + NumFromString(stage.name)));
}
if (!PlayerPrefs.HasKey("Food4Price" + NumFromString(name) + "_" + NumFromString(stage.name)))
{
//Debug.Log("Price4" + NumFromString(name) + "_" + NumFromString(stage.name) + ": " + Food4Price);
PlayerPrefs.SetString("Food4Price" + NumFromString(name) + "_" + NumFromString(stage.name), Food4Price.ToString());
}
else
{
Food4Price = ulong.Parse(PlayerPrefs.GetString("Food4Price" + NumFromString(name) + "_" + NumFromString(stage.name)));
}
}
private void Start()
{
Debug.Log("Price1: " + PlayerPrefs.GetString("Food1Price" + NumFromString(name) + "_" + NumFromString(stage.name)));
Debug.Log("Price2: " + PlayerPrefs.GetString("Food2Price" + NumFromString(name) + "_" + NumFromString(stage.name)));
Debug.Log("Price3: " + PlayerPrefs.GetString("Food3Price" + NumFromString(name) + "_" + NumFromString(stage.name)));
Debug.Log("Price4: " + PlayerPrefs.GetString("Food4Price" + NumFromString(name) + "_" + NumFromString(stage.name)));
}
private int NumFromString(string testString)
{
int testInt = 0;
string newString = "";
for (int i = 0; i < testString.Length; i++)
{
for (int j = 0; j < 10; j++)
{
if (testString[i].ToString() == j.ToString())
{
newString += testString[i];
}
}
}
testInt = int.Parse(newString);
return testInt;
}
if 3 objects have that same script what will happen is
First you delete all keys and create keys for the first object Second object will delete previous keys and create new key for him Third you delete again previous so you end up with only the keys for your object
easiest solution is to move all your code to the Start event and leave only in the awake the dele$$anonymous$$ll method
@xxmariofer exactly that was happening. Thank you so much for your response.