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 Cheetoturkey · Jul 16, 2014 at 01:28 AM · read.txt

Reading a .txt file not working?

For some reason when i try to get it to Load the .txt file it just says that the file could not be found

 using UnityEngine;
 using System.Collections;
 using System.Text;
 using System.IO;  
 
 public class TextCar : MonoBehaviour 
 {
 
     public TextAsset text;
     void Start()
     {
         Load (text.name);
     }
 
     private bool Load(string fileName)
     {
         // Handle any problems that might arise when reading the text
         try
         {
             string line;
             // Create a new StreamReader, tell it which file to read and what encoding the file
             // was saved as
             StreamReader theReader = new StreamReader(fileName, Encoding.Default);
             
             // Immediately clean up the reader after this block of code is done.
             // You generally use the "using" statement for potentially memory-intensive objects
             // instead of relying on garbage collection.
             // (Do not confuse this with the using directive for namespace at the 
             // beginning of a class!)
             using (theReader)
             {
                 // While there's lines left in the text file, do this:
                 do
                 {
                     line = theReader.ReadLine();
                     
                     if (line != null)
                     {
                         // Do whatever you need to do with the text line, it's a string now
                         // In this example, I split it into arguments based on comma
                         // deliniators, then send that array to DoStuff()
                         string[] entries = line.Split(',');
                         if (entries.Length > 0)
                             Debug.Log(entries);
                     }
                 }
                 while (line != null);
                 
                 // Done reading, close the reader and return true to broadcast success    
                 theReader.Close();
                 return true;
             }
         }
         
         // If anything broke in the try block, we throw an exception with information
         // on what didn't work
         catch (IOException e)
         {
             Debug.Log(""+e.Message);
             return false;
         }
     }
 }
Comment
Add comment · Show 1
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 robertbu · Jul 16, 2014 at 02:01 AM 0
Share

I don't know the specific problem, but there are a couple of strange things going on here. First, you are passing 'text.name' to the Load() function. 'text.name' will be the name of the game object. Is your file really named the same as this game object? And is that file at the default path (since you don't use a path) for a StreamReader().

The other strangeness is the use of TextAsset at all. Typically in a script like this, you would put a Text file in the project. Then you would drag and drop that file on the public TextAsset variable ('text' in this case). After doing so, there would be no need for a StreamReader(). You could get the whole text of the file using text.text. To build your lines, you would first have to split the whole text using the '\n' and '\r' characters and then split on commas like you do on line 42.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Saad_Khawaja · Jul 17, 2014 at 09:18 AM

From the code, I'm assuming you want to read the text file line by line.

You don't need to use StreamReader for that. You can simply use TextAsset, split it and then read the string[] line by line.

 public TextAsset TextFile; 
 void readTextFileLines() { 
     string[] linesInFile = TextFile.text.Split('\n');
     
     foreach (string line in linesInFile)
     {
         Debug.Log(line);
     }
     
 }
Comment
Add comment · 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

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

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

creating txt file 2 Answers

Read a specific line from .txt file. 3 Answers

Reading a .txt File 3 Answers

Unity iPhone - Using the file system in my app/game? 1 Answer

Saving My World 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