- Home /
Object Creation
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[i]);
}
// 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);
}
Answer by jonas.du · Sep 28, 2011 at 02:07 PM
If you place your prefabs in a folder called 'Resources' you can load them dyamicly like this:
var rock:GameObject = Recources.Load("rock_001");
Where 'rock_001' is the name of the prefab you want to load. Read this part of the documentation.
Answer by SilverTabby · Sep 28, 2011 at 04:00 AM
It appears that you are looking for either a [Hashtable][1] or a [Dictionary][2]. These are collections of objects that allow you to access them based on a key (usually a string - hey look at that)
Advantages to these types of data structures:
Can store a varibable number of Objects
You can refer to each object via a string, for example "rock_001"
Finding, adding, removing, retrieving elements from the list is easy and efficient
Keep in mind that these have a very high memory overhead in their use.
The example code in the linked reference (near the bottom) is written in C#, so here's how to create and work with a Hashtable in Javascript:
import System.Collections.Hashtable;
var prefabs : Hashtable = new Hashtable();
var rockPrefab : GameObject;
function Start()
{
addEntry( "rock_001", rockPrefab);
Debug.Log( (prefabs["rock_001"] as GameObject).name );
}
function addEntry( key : String, value : GameObject)
{ prefabs[key] = value; }
function createObject( name : String )
{
Instantiate( prefabs[name] as GameObject, transform.position, transform.rotation);
}
One thing you have to be careful about using Hashtables is that you ONLY use one type of objects as keys (String) and that you ONLY put one type of object into it (choose GameObject or Transform and stick with), and that you ALWAYS cast the result of the Hashtable to the correct type of object.
The Dictionary takes care of this problem by using Generics (see the example code at the bottom of the links). The Dictionary makes sure that you never accidentally put a Rigidbody in where a Transfrom should be. [1]: http://msdn.microsoft.com/en-us/library/system.collections.hashtable.aspx [2]: http://msdn.microsoft.com/en-us/library/xfhwa508.aspx
Thanks for the answer. I come from a Java background so I'm familiar with how both Dictionary and Hashtable function but was hoping to avoid using them because of their memory intensiveness. I was hoping to just create the prefabs on the fly verses store "originals" in a data structure and clone them when needed.
Your answer

Follow this Question
Related Questions
How would I change the value of one prefab's variable via script without changing all of them? 1 Answer
When the original enemy dies, the spawns stop (javascript) 2 Answers
Prints correct on console but not updating on Prefab/Gameobject. 2 Answers
Destroying a prefab on collision with a cube? 1 Answer
Instantiating an Object... 1 Answer