Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by KrisCadle · Feb 23, 2012 at 04:24 AM · yxxyzz

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);
 }



Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Owen-Reynolds · Feb 23, 2012 at 04:30 PM 0
Share

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.

avatar image KrisCadle · Feb 23, 2012 at 07:05 PM 0
Share

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

avatar image Owen-Reynolds · Feb 23, 2012 at 07:39 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotating GameObject problem 4 Answers

How to create a GUI Text when reaching a point 1 Answer

I have a GUI Text that shows the X,Y,Z but im not sure how... I made one but It just kept increasing... any advice? 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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges