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 benfattino · May 13, 2012 at 02:50 PM · importexternaltxt

Import .txt and make vector3 array

I try to import a list of coordinate from .txt file (0,0,0,1,1,1,2,3,4,2,2,2 // x,y,z,x1,y1,z1...) and then make a vector3 array to draw line. I'm using this code:

 import System.IO;
 var Filename = "Point.txt";
 var lineMaterial : Material;
 var lineThickness = 1.0;
 var line3D = false;
 private var PointsList : float[];
 private var Lines : Array;
 private var LinesArray : Vector3[];
 private var i : int;
 
 //PointsList = []; // in old version I copy point directly, but now i will do copy directly from .txt external file
 function Start () {
     var sr = new StreamReader(Application.dataPath + "/" + Filename);
     var fileContents = sr.ReadToEnd();
     sr.Close();
     var PointsList = fileContents.Split(","[0]);
     
     Lines = new Array();
     for (i = 0; i < PointsList.length / 3; i++){ 
     Lines.Add(Vector3(-PointsList[i*3],PointsList[i*3+2],-PointsList[i*3+1]));
 } 
     LinesArray = Lines.ToBuiltin(Vector3);
     var line = new VectorLine("Line", LinesArray, Color.white, lineMaterial, lineThickness);
 
     VectorManager.useDraw3D = line3D;
     VectorManager.ObjectSetup (gameObject, line, Visibility.Dynamic, Brightness.None);
 }


Unity give me error:

Assets/Script/Wireframe.js(24,62): BCE0050: Operator '-' cannot be used with an expression of type 'String'. Somebody help me? Thanks.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by whydoidoit · May 13, 2012 at 04:48 PM

You need to convert the strings in the array to floats in your loop line:

Lines.Add(Vector3(-PointsList[i*3],PointsList[i*3+2],-PointsList[i*3+1]));

You can do that using float.Parse(someString).

So something like Lines.Add(Vector3(-float.Parse(PointsList[i*3]), etc.

Comment
Add comment · Show 4 · 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
avatar image benfattino · May 13, 2012 at 05:18 PM 0
Share

thanks for answer. Single.Parse doesn't work. I try with float.Parse and no problem with compilation but when I play scene give me error: !IsFinite(outDistanceForSort) Any idea?

avatar image whydoidoit · May 13, 2012 at 05:36 PM 0
Share

Oops, sorry about that Single thing, will fix the answer. Thinking about the next problem :)

avatar image whydoidoit · May 13, 2012 at 05:42 PM 0
Share

Ok so I'm guessing something in your input string isn't a float - that error would indicate that it was NaN somewhere (Not A Number). I'm not too familiar with Vectrosity, but check your file. (BTW your comment indicates that the file is in the format x,y,z but that's not how you are constructing the Vector3 - which uses the 3rd element in the second location - this is NOT your current problem however).

avatar image whydoidoit · May 13, 2012 at 05:46 PM 0
Share

By the way float.Parse is more aggressive than the alternate Convert.ToSingle(someString) which will do its best - rather than saying the string must be only a float - you know Convert.ToSingle("0 is a number") will return 0 where float.Parse will not

avatar image
0

Answer by benfattino · May 13, 2012 at 08:32 PM

I try another way:

 import System.IO;
 //var Filename = "Point.txt";
 var Filename = String;
 var lineMaterial : Material;
 var lineThickness = 1.0;
 var line3D = false;
 private var LinesArray : Vector3[];
 private var i : int;
 
 function Start () {
     var sr = new StreamReader(Application.dataPath + "/" + Filename);
     var fileContents = sr.ReadToEnd();
     sr.Close();
     var Points = fileContents.Split(","[0]);
     
     PointsList = new Array();
     for (i = 0; i < Points.length; i++){ 
     PointsList.Add(float.Parse(Points[i]));
   }
     Lines = new Array();
     for (i = 0; i < PointsList.length / 3; i++){ 
     Lines.Add(Vector3(-PointsList[i*3],PointsList[i*3+2],-PointsList[i*3+1]));
 } 
     LinesArray = Lines.ToBuiltin(Vector3);
     var line = new VectorLine("Line", LinesArray, Color.white, lineMaterial, lineThickness);
     VectorManager.useDraw3D = line3D;
     VectorManager.ObjectSetup (gameObject, line, Visibility.Dynamic, Brightness.None);
 }

Works. But, if the string start with 0 (for example 0,0,0,1,1,1) unity give me error when play (but it play again):

!IsFinite(outDistanceForSort)

Any ideas?

Comment
Add comment · Show 3 · 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
avatar image whydoidoit · May 13, 2012 at 08:37 PM 0
Share

What is the full version of that error in the console, under the message line there should be a stack trace

avatar image whydoidoit · May 13, 2012 at 08:43 PM 0
Share

You should probably print() each of your Vector3s after parsing to make sure that it is working. It feels a bit like they are all zero or something

avatar image benfattino · May 13, 2012 at 09:10 PM 0
Share

Unity give me only: !IsFinite(outDistanceForSort) But it seem to be work...

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

I'm using Java Script with import System.IO. 1 Answer

How to import an element when the game is build 0 Answers

How do I include added dll's to a android apk build? 1 Answer

How to add text to an external .txt file? 1 Answer

Using an external level editor? 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