• 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 /
  • Help Room /
avatar image
0
Question by kimist · Sep 23, 2018 at 12:08 AM · parsetext file

read text file parse problem

Hi everyone,

I'm asking because after several day of research I m still blocked.

I have a text file with this format:

  C     -0.662479     3.460245     1.908363
  C     0.419934     3.067604     1.127385
  C     0.374974     1.869134     0.409111
  C    -0.769773     1.068668     0.479418

And I want to instantiate a Gameobject with those coordinates, for this I use this code:

 public GameObject atomes;
     public class Atome : MonoBehaviour
     {
 
         public string atome_symbol;
         public string atome_x;
         public string atome_y;
         public string atome_z;
     }
 
     public void Read()
     {
         int i = 1;
         string atome_symbole = "";
         float atome_x = 0.0f;
         float atome_y = 0.0f;
         float atome_z = 0.0f;
 
         string[] separatingChars = { " " };
 
         string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Pauze\Desktop\geometry.txt");
 
         foreach (string linea in lines)
         {
             string line= linea.Trim();
             line = line.Replace("      ", " ");
             line = line.Replace("     ", " ");
             line = line.Replace("    ", " ");
             line = line.Replace("   ", " ");
             line = line.Replace("     ", " ");
             string[] entries = line.Split(separatingChars, System.StringSplitOptions.None);
             
             atome_symbole = entries[0];
             atome_x = float.Parse(entries[1]);
             atome_y = float.Parse(entries[2],System.Globalization.NumberStyles.AllowDecimalPoint);
             atome_z = float.Parse(entries[3]);
 
 
 
             GameObject newatome =  Instantiate(atomes,new Vector3(atome_x,atome_y,atome_z), Quaternion.identity );
             newatome.transform.name = $"{atome_symbole}_{i}";
             GameObject.Find($"{atome_symbole}_{i}").GetComponent<atome>().atome_sym = atome_symbole;
 
             Debug.Log($"{line} + {atome_x} + {atome_y} + {atome_z}");
             i++;
 
                    }
     }
     }

My problem is the split is not working properly. In the consol I have this:

 C 0.419934 3.067604 1.127385 + 0.419934 + 0 + 0
 UnityEngine.Debug:Log(Object)
 dreader:Read() (at Assets/scripts/dreader.cs:61)
 UnityEngine.EventSystems.EventSystem:Update()
 

I try tu use the Try.parse also, didn't work.

does anyone have an idea? I was thinking about changing the format of the file with code before to read it, but if what I'm doing here is not working, I don't think it will work if I try to do it before.

Thanks a lot for any suggest!

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

1 Reply

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

Answer by Hellium · Sep 23, 2018 at 08:53 AM

     for( int lineIndex = 0 ; lineIndex < lines.Length ; ++lineIndex )
     {
         string[] entries = lines[lineIndex].Split(separatingChars, System.StringSplitOptions.RemoveEmptyEntries);

         string atome_symbole = entries[0];
         float atome_x = float.Parse(entries[1]);
         float atome_y = float.Parse(entries[2]);
         float atome_z = float.Parse(entries[3]);

         GameObject newatome =  Instantiate(atomes,new Vector3(atome_x,atome_y,atome_z), Quaternion.identity );
         newatome.transform.name = $"{atome_symbole}_{i}";
         GameObject.Find($"{atome_symbole}_{lineIndex}").GetComponent<atome>().atome_sym = atome_symbole;

         Debug.Log($"{lines[lineIndex]} + {atome_x} + {atome_y} + {atome_z}");
 
     }
Comment
Add comment · Show 2 · 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 kimist · Sep 23, 2018 at 09:51 AM 0
Share

Thanks a lot, I still don't really understand why this is working and not my code ...

avatar image Hellium kimist · Sep 23, 2018 at 10:01 AM 0
Share

Using the System.StringSplitOptions.RemoveEmptyEntries ins$$anonymous$$d of the multiple calls to string.Replace solved many problems.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

151 People are following this question.

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

Related Questions

\n Not picked up when reading string from file's name 1 Answer

How to work with Timestamp data in Unity 0 Answers

Parse RestAPI in Unity WebGL build not working on Google Chrome 1 Answer

Parse issue, despite the data being pulled from database 2 Answers

parsing a double to bigint also getting playerprefs bigint 0 Answers


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