• 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 Drum · Apr 07, 2011 at 08:54 AM · guitextfileguitextread

Problem Reading A Text File

Hi,

I have a script which reads the contents of a text file and then outputs to the GUI. It works fine in the editor, in scene and game view, but when I build and run (either the web player or a Mac standalone) I'm not seeing the text. Am I overlooking anything?

This is the script. The file is just a plain text file and sits in the Resources folder in my Assets folder.

 import System.IO;
 var fileName = "ratfacts.txt";
 var letterPause = 0.2;
 var linePause = 10;
 var sound : AudioClip;
 private var word;
 
 function Start () {
     var sr = new StreamReader(Application.dataPath + "/Resources/" + fileName);
     var fileContents = sr.ReadToEnd();
     sr.Close();
     var lines = fileContents.Split("\n"[0]);
     for (line in lines) {
         word = line;
 //      guiText.text += line + "\n";
 
         TypeText();
         if(word.length > 1){
             yield WaitForSeconds(linePause);
         }else{
             yield WaitForSeconds(2);
         }
     }
 }
 
 function TypeText () {
     for (var letter in word.ToCharArray()) {
         guiText.text += letter;
         if (sound)
             audio.PlayOneShot (sound);
         yield WaitForSeconds (letterPause);
     }
     guiText.text += "\n";
 }

             

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 Fattie · Nov 26, 2015 at 06:45 PM 0
Share

FTR here's the trivial way to use a text file in Unity http://answers.unity3d.com/answers/40481/view.html

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bampf · Apr 07, 2011 at 05:05 PM

The StreamReader constructor that takes a string is expecting a filename. In a webplayer, Application.dataPath will return a URL instead of a filename.

To read text from a URL in Unity, use the WWW class.

Your code can detect if its running in the webplayer or not by checking Application.isWebPlayer. That way you can have it pick which way it needs to read the file.

Edit: Resources.Load loads assets like textures and audio, regardless of whether you are in the webplayer or not. I'm guessing you used that to load textures and audio. It should also be possible to read in your text file that way. The object it returns will be of type TextAsset.

Comment
Add comment · Show 1 · 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 Drum · Apr 07, 2011 at 06:26 PM 0
Share

Ah! I understand now. Thanks Bampf.

For some reason accessing this site from this computer doesn't recognise me as the same drum who asked the question, so I can't mark it as correct. I will give both you guys a vote up and mark correct tomorrow when I'm back in the office.

Thanks again.

avatar image
2

Answer by Eric5h5 · Apr 07, 2011 at 09:14 AM

There is no physical Resources folder in a build. Also, a webplayer can't load external files from disk anyway (only through the web). Sounds like you should just use a TextAsset rather than loading stuff, since you don't seem to need an actual external file.

Comment
Add comment · Show 1 · 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 Drum · Apr 07, 2011 at 10:38 AM 0
Share

I see. I'll certainly look at TextAsset, but I think I'm confused because I have a lot of audio and some imagery in the project which is in the Resources folder and is called in by script in the same way and both work fine on web-player or stand-alone builds. I was assu$$anonymous$$g text would be much the same.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

How to read particular line from *.txt file? 1 Answer

I cant read the file i wrote, unless i reload it in VS. 1 Answer

What's wrong with my function? It keeps telling me 'loseText' is not a member of 'UnityEngine.GUIText'. Please help, please and thank you! 0 Answers

Fix Blurry UI text? 10 Answers

Create GUIText from Javascript 3 Answers


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