dear Community.
i have a .txt file which looks like this.
Name xCoordinates yCoordinates:
box 25 32
table 28 30
…
now i am trying to code a streamreader which recognizes the name (table) and puts an object (for testing, a simple cube) on those coordinates in my game. (z is always 0)
i can’t find a way to “extract” the name and the coordinates…my code so far is down below
i tried with "String[ ] points = Regex.Split(path, " “);” but that does not seem to work.
string path = "//Desktop/asd.txt";
StreamReader reader;
void Start()
{
reader = new StreamReader(path);
}
private void Update()
{
if (path != null)
{
path = reader.ReadLine();
Debug.Log(path);
if(path.Contains("table"))
{
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = new Vector3(0, 0.5F, 0);
}
}
Plain old String.Split should do the job.
What does your code that uses split look like?
private void Update()
{
while (path != null)
{
path = reader.ReadLine();
var Codinates = path.Split(' ');
Vector3[] vectors = new Vector3[Codinates.Length];
if (path.Contains("Quelle"))
{
for (var i = 0; i < Codinates.Length; i++)
{
var pt = Codinates[i].Split(" "[0]);
var x = float.Parse(pt[0]);
var y = float.Parse(pt[1]);
var z = float.Parse(pt[2]);
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = new Vector3(x, y, z);
now i tried it like this but it still does not work… i still get errors and unity does not recognize my numbers as coordinates
What sort of errors?
I thought the first item was a string, not the x coordinate?
var x = float.Parse(pt[0]);// Is this not the name?
ou you are right. it must be
var x = float.Parse(pt[1]);
var y = float.Parse(pt[2]);
var z = 0
i still get an error
IndexOutOfRangeException: Array index is out of range.
txtEinlesen.Update () (at Assets/txtEinlesen.cs:53)
Debug it or use Debug.Log to print out the values and see whats going wrong. Sounds like your split function is not returning enough items.
Print out the items it finds.
var pt = Codinates[i].Split(' ');
foreach(var p in pt)
{
Debug.Log(p);
}
table 1 1
UnityEngine.Debug:Log(Object)
txtEinlesen:Update() (at Assets/txtEinlesen.cs:57)
and a second error which is no concern right now, i guess…
NullReferenceException: Object reference not set to an instance of an object
txtEinlesen.Update () (at Assets/txtEinlesen.cs:43)
so i guess the coordinates schould be 1 1.
Looks like it did not split the text correctly. You sure its separated by spaces and not tabs?
Why are you splitting the text twice? Its quite confusing what you are doing.
Try
for (var path = reader.ReadLine(); path != null; path = reader.ReadLine())
{
var items = path.Split(' ');
string itemLabel = items[0];
if (itemLabel == "Quelle")
{
Assert.IsTrue(items.Length > 3);
float x, y, z;
float.TryParse(items[1], out x);
float.TryParse(items[2], out y);
float.TryParse(items[3], out z);
// TODO: etc
}
}
1 Like
i know its quite confusing. i will try to explain it a little further 
i will try with tabs. i think u are right.
tabs dont work (Assets/txtEinlesen.cs(45,40): error CS1012: Too many characters in character literal.)
the text.file i want to use the data of origins from a table.file of a second program.
it is possible to export the table.file via excel, xml or text. i chose text because i think it is the easiest way for unity to read the data.
the goal is to automatically create a scene with a press of a button (import text.file),
while the text.file defines what objects are placed and where they have to be in the unity-scene. (on a plane with a certain coordinate system / the same system as in the other program). the objects dont have to move or roatate or anything. i just need to “walk” through my scene (placed objects) as it is not possible to have a good 3D-view in the second program.
if i manage to place cubes in the right place for the right words “table / quelle / …” i can exchange the graphics with self made 3ds-max graphics so it gets nice to look at.
WIth @karl_jones example. What issue are you encountering? Other then it looks like you don’t have a z coordinate, so you probably just want to remove the last float.tryParse and set it to 0 as you were trying to do in your code.
You probably also don’t want to do this call in Update. I’m thinking you want to simply place your items once and be done.
If you are stuck still, show us the modified code and maybe just the first 2 or 3 lines of your text file. Also include the error or the debug print outs (if you aren’t getting an error) to show if your results aren’t what is expected.
1 Like