- Home /
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!
Thanks Peter! I tried this, but it wouldn't work with streamreader...
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);
}
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.
Is your object enabled?
Do you ever call the function processTextFiles() from anywhere?
$$anonymous$$aybe try putting it in an Awake() function....
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.
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?
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.
@jahroy, you can't comment on other peoples posts until you get 15 rep.