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 artfish · Oct 21, 2014 at 01:58 AM · iostextrandomloadresource

How do I load my txt file in iOS

Hello! Ok, I'm pretty noobish so here we go. I've been at this for hours now, researching and testing but to no avail. I've got this working just fine in the editor, but when I build to my iPhone, it builds fine, but the text won't load. I'm guessing it's because it can't find the .txt file once it's built. I tried using resources.load, but frankly,I really can't figure this out.

So, I have a giant word list named "wordlist.txt". All words are separated by line breaks. I'm just trying to load a word from the file and pass it to the textmesh on the iOS. Thanks!

 #pragma strict
  import System.IO;
            
     function Start () {
         var fileName = "wordlist.txt";
         var sr = new StreamReader(Application.dataPath + "/" + fileName);
          var fileContents = sr.ReadToEnd();
         sr.Close();
         var mydata = fileContents.Split("\n"[0]);
         // pick a random number between 1 and 10
          var myrandom = Random.Range(1,100000);
 
         GetComponent(TextMesh).text = mydata[myrandom];
 }
    
      

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
1
Best Answer

Answer by robertbu · Oct 21, 2014 at 02:00 AM

If the text file is static (i.e. does not change after the app ships), just drop into your Assets folder and link it in as a TextAsset:

     public TextAsset wordlist;

Just drag and drop your wordlist.txt file on this variable in the Inspector.

Comment
Add comment · Show 4 · 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 artfish · Oct 21, 2014 at 03:12 AM 0
Share

Thanks for the help! Ok, I'm struggling with this. I created a new c# script with the declaration public TextAsset wordlist and added it to my game object. No luck. (obviously because I'm doing it wrong. derp!) Do I need to alter my original script above in anyway or should it now be able to find it? I appreciate the help robertbu.

avatar image robertbu · Oct 21, 2014 at 03:22 AM 0
Share

Your script above could look like this:

 #pragma strict
             
 public var fileContents : TextAsset;            
             
 function Start () {
      var mydata = fileContents.text.Split("\n"[0]);
      // pick a random number between 1 and 10
       var myrandom = Random.Range(1,100000);
 
      GetComponent(Text$$anonymous$$esh).text = mydata[myrandom];
  }

You would take your 'wordlist.txt' file and place it anywhere in Assets but Resources or Strea$$anonymous$$gAssets. Once the above script is attached to a game object, select the game object in the hierarchy and then drag the 'wordlist.txt' file on the 'fileContents' variable in the inspector.

A note about your splitting. If you are authoring on Windows, you will have carriage returns at the end of lines as well, so you may want to trim the result:

  GetComponent(Text$$anonymous$$esh).text = mydata[myrandom].Trim();
avatar image artfish · Oct 21, 2014 at 03:49 AM 0
Share

Bam. Thank you so much. I finally got it with your wisdom. $$anonymous$$uch appreciated!!!

avatar image artfish · Oct 22, 2014 at 01:15 AM 0
Share

Hey robertbu! I've been at this one all damn day and I'm frustrated that I'm here begging for help again. Not Giving up! :D Ok, so I'm implementing another function in my game. I want to take this var, mydata[myrandom], we generate in the above script, and use it with a cs text-to-voice script I'm using. I've read up (A lot) on the order Unity compiles (standard assets folder in first pass, etc) but I've tried a million different wrong ways and I'm getting errors, of course.

In a cs script I would call the function like this - TextToSpeech$$anonymous$$anager.SendTextToSpeech(stringname);

However, now matter what I try, I keep getting errors and can't compile. I'm not sure the best way to go about doing this, call the mydata[myrandom] from a new TextToSpeech$$anonymous$$anager cs script or call the TextToSpeech$$anonymous$$anager fro a js. I really don't care how it's done as long as it get's done. I'm thinking to convert mydata[myrandom] to a string a just call it from a cs script, but no matter what I try, I'm gettting the type or namespace name "randWordGen" (the script above) cannot be found error.

anyone-Thanks in advance for your help!

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

2 People are following this question.

avatar image avatar image

Related Questions

load file from Xcode 1 Answer

GetComponentInChildren is not a member of 'Object' 1 Answer

Save object position (random) 1 Answer

How to load a sprite via WWW on IOS? 1 Answer

The best way to save and load data for mobile in unity ? 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