- Home /
Help with reading a text file?
Why does this script not work? I am getting an error saying "Object reference not set to an instance of an object" reffering to the
var lineElements = lines[currLine]
section of the script. This is probably wrong in every way. But I tried my best to follow the help given to this question.
if (rec.playing) {
if (!readOnce) {
var sr = new StreamReader("recording/Crash.txt");
fileContents = sr.ReadToEnd();
sr.Close();
readOnce = true;
}
var lines = fileContents.Split("\n"[0]);
for (line in lines) {
print (line);
}
}
var lineElements = lines[currLine];
if (lineElements == rec.recTime) {
currLine = (currLine+ 1);
if (RideToggle<1) {
audio.clip = Ride;
audio.Play();
RideToggle=1;
} else {
RideToggle=0;
}
}
I have already written the writing part of this script and I have just got to finish this reading part. Any help will be massively thanked! As a side note how do you add a bounty to a question?
On request:
Script which contains the HUD: All that is important from this script is that it is named "rec" and it has 2 static variables called "recTime" "playing" and "recording" float, boolean, boolean respectively. Playing is true when playback is requested by user and same for recording. recTime is set to zero every time the user begins recording or playing back (then goes up with game time).
Script I want to playback with:
import System.IO; private var readOnce = false; private var fileContents : String; private var currLine = 0; private var lineElements : float; private var lines : String[]; var Ride : AudioClip; var RideToggle=0;
function Update () {
if (Input.GetButtonDown ("crashKey")) {
if (rec.recording) {
var crashSW = new StreamWriter("recording/Crash.txt",true);
crashSW.WriteLine(rec.recTime + "\n");
crashSW.Close();
}
if (RideToggle<1) {
audio.clip = Ride;
audio.Play();
RideToggle=1;
} else {
RideToggle=0;
}
}
if (rec.playing) {
if (!readOnce) {
var sr = new StreamReader("recording/Crash.txt");
fileContents = sr.ReadToEnd();
sr.Close();
readOnce = true;
}
lines = fileContents.Split("\n"[0]);
for (line in lines) {
print (line);
}
}
if (lines==null)return;
lineElements = float.Parse(lines[currLine]);
if (lineElements > rec.recTime) {
currLine = (currLine+ 1);
if (RideToggle<1) {
audio.clip = Ride;
audio.Play();
RideToggle=1;
} else {
RideToggle=0;
}
}
}
Answer by Mike 3 · Oct 26, 2010 at 08:30 PM
You're assigning the split contents to a variable at a lower scope to the code below it - my guess is that you probably added the var in front of lines by accident, and that it's supposed to be declared higher up in the script, at the correct scope
Thankyou, I hadn't noticed that. But I declared the variable at the top of the script as a String and it gave me a different error saying "Cannot convert String[] to String". So I declared the variable as a String[]. Now it is giving me the same error as before...
make sure you removed the word var on that line (var lines = fileContents.Split("\n"[0]); )
Thankyou again for the help so far. I have added the whole script.