string quote;
public TextAsset splashFile;
void Start(){
string dataLines = splashFile.text.Split(’
‘);
string dataPairs = new string[dataLines.Length];
int lineNum = 0;
foreach (string line in dataLines){
dataPairs[lineNum++] = line.Split(’,');
}
quote = dataPairs[Random.Range(0,lineNum++)][Random.Range(0,lineNum++)];
}
First two lines, declare a String and a TextAsset. You don’t mention instantiating the text asset and that answer that you linked to only vaguely mentions that you must drag the text asset (the .txt file) into the slot where it says “splashFile” in the inspector while viewing a GameObject that has your script attached.
It is at this point that I need you to confirm that you have done this because it is a common mistake to make and can sometimes lead to similar errors, but probably not this one.
You indicated that you do not understand what “lineNum++” means: I would suggest that if you do not have any, or much programming experience that you first attempt to learn a language in a more formal (and supported) environment as there are more tutorials for a coder of your level for good 'ole C# than C# in Unity, at your level.
Continuing on for an explanation of the remaining lines:
string[] dataLines = splashFile.text.Split('
');
string dataPairs = new string[dataLines.Length];
The first line (presumably) takes ALLLL of the text from the file and loads it into a string, and then splits it at every instance of the ‘/n’ character. Which is usually the newline. (UnityAns is parsing out the slash, so just pretend that it’s the right direction)
int lineNum = 0;
foreach (string line in dataLines){
dataPairs[lineNum++] = line.Split(','); //This line here
}
“This line here” is splitting each line at the comma.
This means that “Hello,/n”
After those two splits will become: (“Hello” , null )
Note how there is nothing after the comma (the newline is taken off from the first line) and since the elements of the array were previously undefined (null) then the later line.
quote = dataPairs[Random.Range(0,lineNum++)][Random.Range(0,lineNum++)];
May actually throw a nullreference exception. However, since the second dimension of the array is also not set before being accessed by this line it doesn’t have a chance to throw said exception but instead will complain about the array not even having an element to access there.
(It may be confusing how that is different from a nullreference exception, but believe me. . .there is a difference)
Well, that was a wall of text. I hope it’s at least helpful. My suggestion for this code is that it needs some very serious refinement and some refactoring to be more in line with your needs, which need to be more defined for me to fully write a derived version.
Thank you very much for your time.