- Home /
Creating Objects using CSV file
I am trying to create an object( 2D Cube) and I am using a CSV file to get its x y and z coordinates I can read and log the coordinates, but they are string so I use a method to try and convert to float. But when I play the scene the objects are not created.
distance += distanceChange;
Vector3 clickPosition = new Vector3(float.Parse(data_values[0], CultureInfo.InvariantCulture.NumberFormat), float.Parse(data_values[1], CultureInfo.InvariantCulture.NumberFormat), float.Parse(data_values[2], CultureInfo.InvariantCulture.NumberFormat)); //Vector3 clickPosition = new Vector3(Convert.ToSingle(data_values[0]),
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = clickPosition;
Debug.Log(data_values[0].ToString() + " " + data_values[1].ToString() + " " + data_values[2].ToString());
I am using the float.Parse(data_values[0], CultureInfo.InvariantCulture.NumberFormat code to convert to float.
Brother have your issue has been solved? I am working on the same project. Can you help me with this. I just need the code display the cube from the 8 coordinates store in CSV file. Would really appriciate if you can help me or ready to pay you if you are comfortable helping me
Answer by myzzie · Aug 18, 2020 at 11:47 PM
You'll have to either replace the periods with commas or change the cultureInfo
if(float.TryParse(data_values[0], NumberStyles.Any, CultureInfo.InvariantCulture, out float x) &&
float.TryParse(data_values[1], NumberStyles.Any, CultureInfo.InvariantCulture, out float y) &&
float.TryParse(data_values[2], NumberStyles.Any, CultureInfo.InvariantCulture, out float z))
{
Vector3 clickPosition = new Vector3(x, y, z);
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = clickPosition;
Debug.Log(data_values[0].ToString() + " " + data_values[1].ToString() + " " + data_values[2].ToString());
}
It was not able to output the cubes even with this code, unfortunately. one thing i noticed is that when I debug the coordinates, for the first iteration it does X1, Y1, Z1 but then the next line it is X1, Y1, is there something wrong with my code
void ReadCSVFile(){
StreamReader strReader = new StreamReader("Assets/Resources/region - Sheet1.csv");
bool endOfFile = false;
while(!endOfFile)
{
string data_string = strReader.ReadLine ();
if (data_string == null){
endOfFile = true;
break;
}
var data_values = data_string.Split (',');
for (int o =0; o < data_values.Length; o++)
{
Debug.Log("Value:" + o.ToString() + " " + data_values[o]);
}
distance += distanceChange;
if(float.TryParse(data_values[0], NumberStyles.Any, CultureInfo.InvariantCulture, out float x) &&
float.TryParse(data_values[1], NumberStyles.Any, CultureInfo.InvariantCulture, out float y) &&
float.TryParse(data_values[2], NumberStyles.Any, CultureInfo.InvariantCulture, out float z))
{
Vector3 clickPosition = new Vector3(x, y, z);
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = clickPosition;
Debug.Log(data_values[0].ToString() + " " + data_values[1].ToString() + " " + data_values[2].ToString());
}
}
Could you show me your writer as well? or a snippet of your csv file
yes here
-4,0.2,0
-5,0.3,0
-3.1,3.7,0
4.2,3.8,0
-2.3,0.4,0