I have a problem that I cannot seem to solve. I want to fetch a file or files from a URL and parse each line in said file(s) to create new objects in my game.
A line looks like this:
rock_001,1.5,0,.01,0,0,0,1,1,1
where
rock_001 is the prefab name
the next 3 elements (1.5,0,.01) are the position
the 2nd set of 3 elements (0,0,0) is the rotation
and the 3rd set of 3 elements (1,1,1) is the scale
I’ve successfully fetched and parsed the file from the URL then I pass it off to the following function.
I have the following code that works but it basically creates an empty game object (object with no substance - mesh, colliders, and certainly not the prefab as defined by info[0], etc).What I’m having difficulties with is info[0] contains the “rock_001” value in which I have a prefab called that. Is there a way to instantiate this prefab? I would rather not create giant if else/case statement for all the prefabs possibilities.
static function CreateObject(line : String) {
// define the variables
var position : Vector3;
var rotation : Quaternion;
// parse the line into it's elements
var info = line.Split(","[0]);
// simple debug output to verify elements of the input line
for(var i = 0;i<info.length;i++) {
Debug.Log("info["+i+"]: "+info*);*
- }*
// define variables based on the input line
- var newObj : GameObject = new GameObject(info[0]);*
- var px : float = parseFloat(info[1]);*
- var py : float = parseFloat(info[2]);*
- var pz : float = parseFloat(info[3]);*
- var rx : float = parseFloat(info[4]);*
- var ry : float = parseFloat(info[5]);*
- var rz : float = parseFloat(info[6]);*
//create position and rotation based on the input line elements
- position = Vector3(px,py,pz);*
rotation = Quaternion.Euler(rx,ry,rz);
//assign the position and rotation to the new object
-
newObj.transform.position = position;*
-
newObj.transform.rotation = rotation;*
-
Debug.Log("newObj: "+newObj.ToString);*
}