So I’ve been trying to translate some javascript xmlreader stuff for a conversation tree, I’ve pretty much got it, I think. The problem I’m having is with my ConvoNodes class and getting the variables in it defined(I think that’s the right term).
I declare ConvoNodes at the top
public class ConvoNodes{
public string prompt;
public string[] answers;
public int[] links;
}
Then I have an XmlReader run through and find the number of elements in my .xml file so that I can set the correct size for the arrays. After I have the counts for everything (which I have exhaustively tested with Debug.Log so I know it works), I define the variables using the counts I have obtained for the size.
convos = new ConvoNodes[promptCnt];
for(int cnt = 0; cnt < convos.Length ; cnt++){
int a = numberOfAnswersPerPrompt[cnt];
convos[cnt].answers = new string[a];
convos[cnt].links = new int[a];
}
I know that convos works just fine, its when i move on to answers and links that I get “NullReferenceException: Object reference not set to an instance of an object”.
I’ve been banging my head against this for awhile and I feel like it’s just something dumb I’ve missed and I’d really appreciate any help.
You need to initialise both the size of the array (which you do) and the members of that array. So in your for loop you need:
convos[cnt] = new ConvoNodes();