- Home /
Question by
paktofu · Nov 30, 2013 at 06:32 PM ·
javascriptarraysarray of gameobjects
Instantiate multiple objects from an array
Hello, I am looking to instantiate a series of objects from a parsed CSV file. I am able to print the parsed data however I am not really sure how to instantiate them based on the CSV file (x y locations for example). Please, any direction or help would be appreciated as I am still very new to coding in general.
The entire code can be found below here:
var csvFile : TextAsset;
var cube : GameObject;
var instantiate : GameObject;
var x = new Array();
var y = new Array();
var lon = new Array();
var count = new Array();
var id = new Array();
var j = 0;
function Start(){
Longitude();
printDebug();
print();
}
function Longitude () {
var stringArray = csvFile.text.Replace("\n"[0],","[0]).Split(","[0]);
for(var i = 2; i < stringArray.length; i+=1){
switch (i%2)
{
case 0:
x[j] = stringArray[i];
break;
case 1:
y[j] = stringArray[i];
j = j + 1;
break;
}
}
}
// Test to see if the array in longitude can be accessed from without. Eventually this code needs to spawn the prefabs
function print(){
for(var z = 0; z < x.length; z+=1)
{
var cube : Transform;
Instantiate(cube, x[z].position, Quaternion.identity);
print("The coordinates for line " + x[z] + " are " + y[z] + ".");
}
}
Comment
Answer by chrisall76 · Nov 30, 2013 at 09:09 PM
I have no clue how you go about getting position, but what I would do is read the x, y, and z positions saved in the file, put then in a array, and instantiate for every object in the array.
var x = new Array();
var y = new Array();
var z = new Array();
for(var i = 0; i < x.length; i += 1){
Instantiate (prefab, Vector3(x[i], y[i], z[i]), Quaternion.identity);
}
Hmm, I tried this, and now it is giving me this error:
InvalidCastException: Cannot cast from source type to destination type.