• 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
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 t$$anonymous$$s 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 t$$anonymous$$s I use t$$anonymous$$s 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 t$$anonymous$$s:

 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 t$$anonymous$$nking 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 t$$anonymous$$nk it will work if I try to do it before.

Thanks a lot for any suggest!

Comment

People who like this

0 Show 0
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
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

People who like this

0 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 instead 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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Format Exception when trying to parseFloat(String) 2 Answers

I cant Multiply integer from Input Field 1 Answer

Error CS0118: Armor.DisplayName is a property but a type was expected 0 Answers

Share text files during run time between games, with out using social media. 0 Answers

Parse a midi file! 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