- Home /
SetString error with PlayerPrefsX
Hello,
I am using the ArrayPrefs2 script from wiki (PlayerPrefsX.js). When I'm trying to save some strings in an array I get the following error.
"NullReferenceException: Object reference not set to an instance of an object PlayerPrefsX.SetStringArray (System.String key, System.String[] stringArray) (at Assets/Standard Assets/Scripts/PlayerPrefsX.js:175)"
Here's the line what I'm using:
PlayerPrefsX.SetStringArray ("Diary_text_1", diary.story_text);
story_text is a string array (`var story_text = new String[21];`) where I want to store some strings and what I want to save.
Does anyone has any idea what could be the problem here? Thanks in advance.
Answer by Eric5h5 · Sep 14, 2011 at 04:15 PM
diary.story_text
is probably wrong; see here for accessing other game objects.
story_text is a global variable in my diary.js script and I didn't have any problems accessing it like this in my other scripts.
In the error message it shows that it might be a problem at the 175th line in the PlayerPrefsX script. In that line of the script is this: if (stringArray[i].Length > 255) I don't know how could this be the problem, since the strings that I am trying to save none of them is longer than 40. Or is it that the whole array cannot be longer than 255?
stringArray[i].Length
is the length of the string for entry i
. stringArray.Length
would be the length of the array. In that case I would guess you're trying to save null entries in the array.
Answer by Selene · Sep 15, 2011 at 02:32 PM
I managed to solve the problem. The problem was indeed with the story_text variable, although the issue wasn't that it couldn't be accessed. (I was assigning some other variables to that and was trying to save those and not the actual array.)
Now I have another question. Can you tell me how can I check if a string key already exists? I would need this so if it exists only then I will load it. Thanks.
Your answer
Follow this Question
Related Questions
My MonoBehaviour Doesnt seem to have PlayerPrefsX..? 1 Answer
PlayerPrefsX IntArray 1 Answer
How to clone an ArrayList? 1 Answer
PlayerPrefsX delete all values? 1 Answer
Saving Array Objects in Android. 0 Answers