- Home /
create game object on the fly
hi, i am using this meshserializer from http://www.unifycommunity.com/wiki/index.php?title=MeshSerializer2 there is a loading mesh using WWW. that is getting a mesh from a url, and put it onto a gameobject.
on that webpage, the tutorial is adding a mesh filter component into an already created game object. but what i am trying to do a little bit different,
i want to create a new game object for each mesh that i downloaded using WWW. that is why i need to create a new game object for each of them.
my code so far is like this. but it is not doing what i thought it would do. the results are just two meshes instead 4 meshes. and the hierarchy is not correct either, and it has "(clone)" on created game object when i ran the game.
function Start() { var urls = new Array (); urls.Add( "http://localhost/akubz_shards/game/assets/bodyMale.data" ); urls.Add( "http://localhost/akubz_shards/game/assets/male_hair-1.data") ; urls.Add( "http://localhost/akubz_shards/game/assets/male_hair-2.data" ); urls.Add( "http://localhost/akubz_shards/game/assets/male_hair-3.data");
log = GameObject.Find("LogMessages").GetComponent(GUIText);
var container = GameObject.FindGameObjectsWithTag("charContainer");
for(var i=0;i<urls.length;i++){
var download = WWW(urls[i]);
yield download;
var mesh = MeshSerializer.ReadMeshFromWWW( download );
if (!mesh)
{
log.text += "Failed to load mesh \n" ;
continue;
}
log.text += "\nMesh loaded from " + urls[i];
var newGO=GameObject.Find("emptyCharContainer");
Instantiate(newGO, transform.position, transform.rotation);
var meshFilter : MeshFilter = newGO.GetComponent(MeshFilter);
if( !meshFilter ) {
meshFilter = newGO.AddComponent(MeshFilter);
newGO.AddComponent("MeshRenderer");
log.text += "\n filter " + urls[i];
}
meshFilter.mesh = mesh;
//make it a children of current object
newGO.transform.parent = transform;
log.text += "\nFinish Loading mesh from " + urls[i];
}
}
Answer by Tetrad · Jul 11, 2010 at 08:05 PM
and it has "(clone)" on created game object when i ran the game.
Every time you instantiate a prefab (or game object), it'll append " (clone)" to it. You can "fix" that by setting the name yourself. Just store off the name of the object you're instantiating from, and set the .name
property on the new one after you instantiate it.
Alternatively, if you just want an empty game object, you can new
one off.
GameObject newEmptyGameObject = new GameObject( "gameObject name" );
Answer by Ashkan_gc · Jul 11, 2010 at 04:27 PM
if you want to create the object out of nothing just create a new GameObject and use AddComponent to add what you want to it. your current code seems correct. there might be a problem in your URLS or ...
note: if you add a component using AddComponent method, unity will add all required components automatically