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 robottomo · Feb 25 at 11:21 AM · string.split

String to float conversion decimal problem

I am importing .txt data containing XYZRGB values generated with Matlab into Unity. Even though RGB values seem odd to me, when manually checked, colors are correct. When I run the whole program, float values of colors and positions are not ok. I can change the position so it matches the values in .txt data with some dividing but can't do that for rgb values (see attached picture). Here is the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.Text;
 
 public class GenerateBalls : MonoBehaviour
 {
     private GameObject colormap;
     List<xyzdata> xyzdata = new List<xyzdata>();
 
     // Start is called before the first frame update
     void Start()
     {
         TextAsset XYZRGB = Resources.Load<TextAsset>("XYZRGB"); // Load text asset (asset is in string)
         string[] data = XYZRGB.text.Split(new char[] { '\n' }); // Split by new lines, create array 
         Debug.Log(data.Length);
 
         for (int i = 0; i < data.Length; i++) // Read each line
         {
             string[] row = data[i].Split(new char[] {','}); //Split on commas
             xyzdata q = new xyzdata();
 
             float pos_x = float.Parse(row[0]);
             float pos_y = float.Parse(row[1]);
             float pos_z = float.Parse(row[2]);
             float col_r = float.Parse(row[3]);
             float col_g = float.Parse(row[4]);
             float col_b = float.Parse(row[5]);
 
             // For unity console
             q.xdata = row[0];
             q.ydata = row[1];
             q.zdata = row[2];
             q.r = row[3];
             q.g = row[4];
             q.b = row[5];
             xyzdata.Add(q);
 
             colormap = GameObject.CreatePrimitive(PrimitiveType.Sphere);
             colormap.transform.position = new Vector3(pos_x, pos_y, pos_z);
             colormap.GetComponent<Renderer>().material.color = new Color(col_r, col_g, col_b);
             colormap.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
         }
         
         foreach (xyzdata q in xyzdata)
         {
             Debug.Log(q.r);
         }
     }
 }

Here is the link to the picture of the problem and original .txt file: https://we.tl/t-nih1xaZW1N

So the question is, how can I modify my script so that the XYZ and RGB values will be executed according to values in imported text file.

Comment
Add comment · Show 4
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 paynob · Feb 25 at 03:28 PM 0
Share

Sorry m8, can't read the samples. Can you wrote some lines?

avatar image robottomo paynob · Feb 28 at 07:36 AM 0
Share

I uploaded the link with the files. But basically when I am changing line 41 and putting in values manually like (0.5, 0, 0) colors are correct, but when I put in variables (col_r, col_g, col_b) then colors do not appear correctly. Same think I noticed in line 40 with positions which are not in -0.35, -0.85, -0.0958 format but -35,-85,-95845. I fixed this with some dividing but can't to that for colors.

avatar image xxmariofer robottomo · Feb 28 at 09:06 AM 0
Share

when you log col_r col_g col_b what outputs?

Show more comments

1 Reply

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

Answer by xxmariofer · Feb 28 at 11:22 AM

I have had this issue in the past, in my case was because I am from spain, and in my place floats decimals where with ',' instead of '.' (or vicebersa dont remember) try something like this

 float.Parse(row[0], CultureInfo.InvariantCulture);
Comment
Add comment · Show 1 · 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 robottomo · Feb 28 at 12:01 PM 0
Share

It's working! Thank you very much.

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

133 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

Related Questions

String.Split Not Splitting Correctly. Some Duplicates are being deleted. 1 Answer

Problem splitting up strings 2 Answers

How to split a string using unity flash player? 0 Answers

Grab part of a string by looking for keyword 2 Answers

String.Split("¦"[0]) is not finding character 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