- Home /
Figured it out
C# array not behaving as expected
I have a c# array that is set to a size of 10, but when i try to access the index 0 of the array in a loop, it gives me an IndexOutOfRange error. When i print the same thing in the Start method, it somehow works. This is the code that produces the error:
public void setLevelRating(int level, string newRating) {
Debug.Log(level);
levelRatings.SetValue(newRating, level); //error occurs here, even when i use levelRatings[level]
string tempString = "";
for(int i = 0; i < levelRatings.Length; i++) {
if(i == levelRatings.Length - 1) {
tempString += levelRatings[i];
} else { tempString += levelRatings[i] + "|"; }
}
PlayerPrefs.SetString("level ratings", tempString);
Debug.Log(PlayerPrefs.GetString("level ratings"));
}
This is the code that calls this method and gives it a level:
void Start() {
dm = GetComponent<DataManager>();
dm.setLevelRating(0, "Professional");
}
How are you declaring levelRatings? Where is the code for levelRatings.SetValue() ?
never$$anonymous$$d I just figured it out. The second code snippet was called from a start method in another object, so it was trying to access the methods in the first code snippet that weren't finished initializing.
Follow this Question
Related Questions
Error CS0029 Help? (Screenshot of Exact Error) 1 Answer
Random NullReferenceException 0 Answers
NullReferenceException error in an array of objects 0 Answers
Distribute terrain in zones 3 Answers
Object reference not set to an instance of an object 1 Answer