• 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 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

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

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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

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

error CS1041: Identifier expected 1 Answer

Reading .DAT file 0 Answers

How to fix this parsing error? 2 Answers

Parse string value to add to enum List 2 Answers

(Unity5, Parse.com) Unable to access Parse query list items 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