- Home /
Question by
motti10 · Dec 29, 2020 at 06:20 PM ·
vector3filestring.split
How do I loop through a Text file to build array of 2 objects per line in file?
Been trying this today without success
In my text file I m loading, I have the following lines
-0.4, 0.0, 1.4 : tv 0.3, -0.3, 0.4 : bathroom 0.4, -0.1, 0.1 : kitchen 0.2, -1.7, 1.4 : mancave etc I am trying to loop through each line and have
I tried :
string[] array = text.Split(':');
position = StringToVector3(array[0]);
foreach (string token in array)
//for (int i = 0; i < lines2.Length ; i++)
{
print(token);
GameObject go2 = Instantiate(prefab, new Vector3(position), Quaternion.identity);
//str = gst.GetComponent<Text>().text;
}
But it fails on position which I thought I could assign to array[0] which is converted from StringToVector
ANy advice, help would be appreciated.
Comment
Best Answer
Answer by Hellium · Dec 29, 2020 at 06:47 PM
Code not tested
string[] lines = text.Split('\n');
for(int i = 0 ; i < lines.Length ; ++i)
{
string[] array = lines[i].Split(':');
Vector3 position = StringToVector3(array[0].Trim());
GameObject go = Instantiate(prefab, position, Quaternion.identity);
go.name = array[1].Trim();
}
Your answer
Follow this Question
Related Questions
.txt file to string[] brakes by line 1 0 Answers
Point on mesh 0 Answers
Why does my vector pan my character slightly to the side? 1 Answer
How do i instantiate a Vector3[] ?? 2 Answers
Position gameObjects based on children 0 Answers