- Home /
How can I get x, y, z spawnpoints from a csv or text file?
Ok, I have a text file with coordinates separated by commas that looks like this:
234,252,435,234,334,345 ... and so on...
I want to use that as a TextAsset and split it into an array (I'm guessing)
var stringArray = myTextFile.text.Split(","[0]);
Then I need to split that again into each set of 3 as x,y,z,x,y,z,x,y,z,...
Then instantiate a prefab at each set of x,y,z, etc...
Basically I want to instantiate a whole bunch of prefabs and get their positions from a simple csv file (which I want to get streaming from outside data eventually, hence no line breaks)
Can anyone please help me figure out how to do this?! I've read the docs till my eyes are red...
This is totally wrong and I've tried a hundred versions of it but here is my code. I'm trying to keep it short...
var myTextFile : TextAsset;
var pixel : Transform;
function Start () {
MakeShape();
}
function MakeShape () {
var stringArray = myTextFile.text.Split(","[0]);
for ( var i = 0; i < stringArray.length; i ++ ) {
x=i; i++;
y=i; i++;
z=i; i++;
}
transform.position = Vector3(x, y, z);
Instantiate (pixel, transform.position, Quaternion.identity);
}
Whatever you do, toss in Debug lines. For a start, after the split: for(...) { Debug.Log(stringArray[i]);
$$anonymous$$aybe you have an off-by-one -- an extra zero, or the first line is always skipped.
Then toss in Debug.Log("x/y/z="+x+"/"+...);
after each read. Once you can "watch" it run, you can see the problem.
Also, another trick is to use a format like "1,2,3/4,5,6/" split on "/" and split the chunks on ",". Easier to check the file, and handles missing coords better.
yeah, I'm trying that and still can't get it to work. I can make it spawn the right number of "pixel" (1 pixel for every set of 3 numbers) There has to be a real simple way of doing this. Turning a string into a series of x,y,z, positions. I just can't figure out the method for using the first 3 numbers of the string (245,262,242) as x,y,z, then spawn, then take the next 3 numbers and make them another x,y,z, and spawn, etc all the way through the string...
I think it should go:
x = i;
then go to the next number in the string (i+)?
y = i;
then go to the next number
z = i;
then spawn at these coordinates
then go to the next number in the string x, y, z, spawn etc... repeat through the string
I feel stuck
The answer below is correct: "i" will always be 0,1,2,3...not the numbers you want. You want to look at the number at position i, which is stringArray[i]
.
For what it's worth, I give similar problems (plus a bit extra) to 1st year college ComSci majors after about 10 weeks. It's not super-easy.
Answer by Eric5h5 · Feb 23, 2012 at 05:49 AM
You need to convert the strings to integers. You can use parseInt, but that will fail if the input accidentally includes non-integers, so it's best to use TryParse. (Also you need to refer to stringArray[i] rather than just i, and your loop won't work right since i gets incremented twice at the end of each iteration.)
Your answer
Follow this Question
Related Questions
Rotating GameObject problem 4 Answers
How to create a GUI Text when reaching a point 1 Answer
y and z rotation axis doing the same thing when x is 270 degrees 0 Answers
spin only on one axis 1 Answer