- Home /
Problem reading csv file when last semicolon is missing
I have a csv file with this text inside
Cube;Mat_3;
Sphere;Mat_2;
Cylinder;Mat_6;
This is the script that load the file, reads its content and then assigns the materials to the relative objects.
//Variables
private var update : int;
private var textAsset : TextAsset;
private var oggetti = new Array ();
private var materiali = new Array ();
private var linee : String[];
private var i : int;
function Start () {
//initialization
update=1;
i=0;
}
function Update () {
//We need to update?
if (update==1){
//get text from a csv file
textAsset = Resources.Load("settings", TextAsset);
linee = textAsset.text.Split("\n"[0]);
//handle the content in 2 string[] (object names, material names)
for(i=1; i<linee.Length ; i++){
if(linee[i]!=""){
oggetti.Push(linee[i].Split(";"[0])[0].ToString());
materiali.Push(linee[i].Split(";"[0])[1].ToString());
}
}
//for each object element set the material if the file exist
for(i=0; i<oggetti.length ; i++){
//here is my problem
if(Resources.Load(materiali[i], typeof(Material)) as Material){
gameObject.Find(oggetti[i]).renderer.material = Resources.Load(materiali[i], typeof(Material)) as Material;
}
}
//we don't need to update again
update=0;
}
}
My problem is: the csv file need, for each column, a final semicolon to load the right material, if the semicolon is missing, the material won't be loaded.
Example row:
Cube;Mat_2
Resouces.Load(materiali[i]) won't find the material
Example row:
Cube;Mat_2;
Resouces.Load(materiali[i]) will find the material
the strangest thing is that if I debug.log(materiali[i]) the console will output the same result. Using ToString() on materiali[i] doesn't helps, Excel saves the csv files without the last semicolon, so I need to add it by hand. There is a solution to make it work without manipulate the file? Thanks
Your answer
Follow this Question
Related Questions
UnityEngine.DefautAsset as TextAsset 1 Answer
How do I find a specific file type with getfiles? 1 Answer
Automatic CSV to TXT conversion [solved] 1 Answer
check for word in dictionary 3 Answers
ReadLine to a Variable 2 Answers