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:
//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).
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("
"[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*);*
*}*
_/* convert the array to a builtin array for fun */_
*var fastFloatArray : float [] = floatArray.ToBuiltin(float);*
*}*
*```*