Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Will 7 · Oct 26, 2010 at 08:22 PM · texttimenullreferenceexceptionfilestreamreader

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&lt;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 &gt; rec.recTime) {
     currLine = (currLine+ 1);
     if (RideToggle&lt;1) {
         audio.clip = Ride;
         audio.Play();
         RideToggle=1;
     } else {
         RideToggle=0;
     }
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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

Comment
Add comment · Show 12 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Will 7 · Oct 26, 2010 at 08:41 PM 0
Share

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...

avatar image Mike 3 · Oct 26, 2010 at 08:43 PM 0
Share

make sure you removed the word var on that line (var lines = fileContents.Split("\n"[0]); )

avatar image Will 7 · Oct 26, 2010 at 08:52 PM 0
Share

Did do, but unfortunately it still isn't working.

avatar image Mike 3 · Oct 26, 2010 at 09:36 PM 0
Share

$$anonymous$$ay want to post the full script in that case

avatar image Will 7 · Oct 27, 2010 at 11:14 AM 0
Share

Thankyou again for the help so far. I have added the whole script.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Why is reading some text files with JS so darn slow?! (title changed to reflect answers) 1 Answer

Read and interpret data from .txt in real time, line by line 0 Answers

parsing csv file 2 Answers

txt file line endings differ from policy settings 1 Answer

How to get info from a text file 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges