- Home /
Unity reading decimals
Hello,
I have this code that I inherited from a friend,( that I am also asking) but in case he is too busy I was hoping maybe this is a simple problem someone here can answer.
Basically I have this code that can read a CSV file and instantiate the data based on x y z and scale of y. But the problem I am having is that it is not reading decimal points. Does anyone know if this is a simple fix ?
Here is the code:
using UnityEngine;
using System.Collections;
public class Cube : MonoBehaviour {
//This code is used to parse a CSV file and also to spawn objects based on that CSV fi
// Big thanks to Seth Iacangelo aka Dark Shot of FF XIV
public TextAsset csvFile;
public GameObject prefab;
private int index = 0;
private int[] x;
private int[] y;
private int[] z;
void Start(){
Longitude();
}
void Longitude () {
print (csvFile);
string[] numberArray = csvFile.text.Replace("\n"[0],","[0]).Split(","[0]);
int length = (numberArray.Length - 1) / 3;
x = new int[length];
y = new int[length];
z = new int[length];
for(int i = 0; i < numberArray.Length - 1; i++){
switch (i%3) {
case 0:
x [index] = int.Parse (numberArray [i]);
break;
case 1:
y [index] = int.Parse (numberArray [i]);
break;
case 2:
z [index] = int.Parse (numberArray [i]);
string name = index.ToString();
GameObject instance = Instantiate(prefab) as GameObject;
instance.transform.position = new Vector3(x[index],y[index],0);
instance.transform.localScale += new Vector3(0,z[index],0);
instance.name = name;
index ++;
break;
}
}
}
}
You've got x,y,z declared as ints, and are using int.Parse
to read them. Did the original code read ints only? And you're wanting to modify it to read floats?
Some of the problem may be that the code is a little wonky. The array isn't used for anything and the switch seems needlessly confusing (why not just read x,y,z together, and have the loop advance by i+=3
?) And when do you want x&z scale to be 0, and not 1?
Answer by DaveA · Dec 03, 2013 at 05:45 PM
Use float.Parse instead of int.Parse
Thank you, this is why I am new to coding. DUH lol why didn't I think of that. It works now :)