Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by WaterWings · Jun 18, 2011 at 07:08 PM · arraysparsingtextassetcsvstring.split

ReadLine to a Variable

I have a .txt file (filled with a string of numbers each on a separate line). I can read this fine and print it to the console window. I now want to take those numbers and get them into where the question marks are below:

 transform.Translate(Vector3(0,0,??)*Time.deltaTime); 

The code I have so far is:

 //import System; (I am not sure when I need to import this?)
 import System.IO; 
 
 print(Application.dataPath);
 var wordFile = "positions2.txt";
 print(Application.dataPath+"/"+wordFile);

 
 function Start () {
 var sr : StreamReader = new StreamReader(Application.dataPath +"/"+ wordFile);
 
 line = sr.ReadLine();
         while (line != null) {
             print(line);
             line = sr.ReadLine();
         }
         sr.Close();
         
         }
 
 //function Update () {
 //transform.Translate(Vector3(0,0,sr)*Time.deltaTime);
 //}

Advice on how to get the numbers into a variable that I can then use for position, etc. would be very helpful. As basic a possible explanation would be great, since I am very new:)

Also, any suggestions for best practice (e.g. if I should do something in a different way would be very helpful).

Thanks ever so much!

Comment
Add comment · Show 2
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 Peter G · Jun 18, 2011 at 07:40 PM 0
Share

have you tried using float.Parse()?

avatar image WaterWings · Jun 19, 2011 at 11:18 AM 0
Share

Thanks Peter! I tried this, but it wouldn't work with streamreader...

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by jahroy · Jun 18, 2011 at 09:34 PM

I would use a TextAsset rather than System.IO if possible.

Here's an example that takes a comma delimited file and stores the numbers it finds in an array:

/* * To create a TextAsset, create a text * file and drag it into your Unity project * * (somewhere in your assets folder) * */

var myTextFile : TextAsset;

function processTextFile () {

 /* create an array to store the floats we find in the file */

 var floatArray = new Array();


 /* split the text file by newline characters */

 var lineArray : String [] = myTextFile.text.Split("\n"[0]);


 /* loop over each line in the file */

 for ( var thisLine : String in lineArray ) {

     /* split each line by commas */

     var numberStrings : String [] = thisLine.Split(","[0]);

     /* loop over the numbers in this line */

     for ( var thisNumber : String in numberStrings ) {

         /* parse the string into a float */

         var someFloat : float = float.Parse(thisNumber);

         print("Found this float: " + someFloat);

         /* put the float into an array you can use later */

         floatArray.Push(someFloat);
     }
 }

 print("I found " + floatArray.length " + numbers: ");

 for ( var i = 0; i < floatArray.length; i ++ ) {
     print(floatArray[i]);
 }

 /* convert the array to a builtin array for fun */

 var fastFloatArray : float [] = floatArray.ToBuiltin(float);    

}

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
avatar image
0

Answer by WaterWings · Jun 19, 2011 at 10:11 AM

Thanks ever so much! I have tried this but nothing seems to be processing, so I am not sure what I've done wrong.

1) I've created a .txt file (with a series numbers delimited by commas (1,2,3,4,5 etc) and dragged the textfile into Unity Assets 2) I've attached the code you supplied to a game object (removed this bit: " + numbers: " ) 3) I've dragged the .txt file into the My Text File field in the inspector

when I hit play, nothing appears in the console. Any ideas on what I have done wrong?

Thanks for all of your help.

Comment
Add comment · Show 5 · 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 jahroy · Jun 19, 2011 at 10:56 AM 0
Share

Is your object enabled?

Do you ever call the function processTextFiles() from anywhere?

$$anonymous$$aybe try putting it in an Awake() function....

avatar image jahroy · Jun 19, 2011 at 10:57 AM 0
Share

You should only post an answer if you are answering the question.

If you are making comments about other answers, you should add a comment ins$$anonymous$$d of another answer.

avatar image WaterWings · Jun 19, 2011 at 11:18 AM 0
Share

Oh, groan (figured it out!) I guess I have to use function start() or function update() ins$$anonymous$$d of the function name?

Or, would I put that function within the update function?

avatar image jahroy · Jun 19, 2011 at 11:43 AM 0
Share

Putting it in the Update function would cause it to execute every frame. That would be bad. Putting it in Start() or Awake() will cause it to execute once. That would be good.

avatar image Peter G · Jun 19, 2011 at 12:00 PM 0
Share

@jahroy, you can't comment on other peoples posts until you get 15 rep.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

.txt file to string[] brakes by line 1 0 Answers

Problem reading csv file when last semicolon is missing 0 Answers

How do I find a specific file type with getfiles? 1 Answer

UnityEngine.DefautAsset as TextAsset 1 Answer

Why does unity expect a file when I try to put text on a mesh? 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