- Home /
PlayerPrefs.GetString returns wrong value
Hi, I've got a strange problem.
I save a string in a database and i want to read these data out of this DB.
My string is saved in the DB - i took a look at it and key + value are written in.
Now I'm using the PlayerPrefs.GetData-Method to get my data and it returns always an empty string ( "" ).
This is the code I use to write in the DB:
PlayerPrefs.SetString("AchievmentsPanty", saveString);
saveString is a string, it's value is like "1,2,3:1,1,0:0,0,0". My DB contains:
To get the data I use this funktion:
string tmpAchievmentsPanty = PlayerPrefs.GetString("AchievmentsPanty", "");
The second value is the default-value (http://docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.GetString.html).
tmpAchievmentsPanty contains always "" - an empty string....
Do you know why this happens and how to get the right data out of the DB?
Answer by Sundar · Aug 24, 2012 at 03:40 PM
You have misunderstood the function - use this
string tmpAchievmentsPanty = PlayerPrefs.GetString("AchievmentsPanty");
What's the difference between your way to call the function and his? You just omit the default value which is by default "". So it's actually exactly the same...
http://docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.GetString.html
Thank you. Was struggling why is PlayerPrefs returning empty string all the time. I was just using the wrong parameter. Cheers
Thank you! Apparently a space in the PlayerPref name bugged the situation over here.
Answer by Bunny83 · Aug 24, 2012 at 03:37 PM
I'm a bit confused, on the one hand you talk about a DB you're using, on the other hand you talk about "PlayerPref"erences. How is the database related to PlayerPrefs? Are you sure that you store the string with PlayerPrefs.SetString before you call PlayerPrefs.GetString?
Keep in mind that each platform has it's own playerprefs, so setting a playerprefs value in the editor, it won't be set in a standalone build or a webbuild.
Answer by Bregnar · Aug 24, 2012 at 04:23 PM
Okay, fixed it.
My script worked well - the only problem was, that I deleted the key at start() because of testing around last night. Thank you all :)
Your answer
Follow this Question
Related Questions
Reading XML Data C# 2 Answers
Cannot read or write a txt file after build 2 Answers
Reading files in Unity from different platform 2 Answers
Reading - Writing data to - from file? 2 Answers
Read string from other script (C#) 1 Answer