- Home /
Create prefab from gameobjects at runtime
So I created a system by which you can attach gameobjects to one another in a "lego" type fashion. the first placed block is considered the "core" piece. once I have placed all the blocks I want on the 'model', I then make a prefab of the model and add it to my inventory, to be instantiated whenever the inventory object is placed on the terrain again.
my dilemma is this, once the model is assembled and saved to a prefab, I check the prefab in the inspector and all the mesh filters have lost their meshes. When the model is instantiated the colliders are all in place correctly but none of the gameobjects have meshes. Other than the meshes disappearing everything seems to be working according to plan. My question is this. Could the problem have something to do with the gameobjects' meshes having the same name ("default"), or could it have something to do with the fact that they are OBJ and not FBX files?
here is my code `
public static Model CreateModel(string modelname, GameObject core){
Model model = new Model();
//GameObject ngo = new GameObject(modelname);
core.AddComponent("Model");
//core.AddComponent("MeshMerger");
//core.transform.parent = ngo.transform;
//core.transform.localPosition = Vector3.zero;
model = core.transform.GetComponent();
model.Name = modelname +"_" +modelcount;
model.Core = core;
//model.Core.name = model.Name;
model.PieceCount = model.Core.transform.childCount+1;
model.BBGameObject = GetModelParts(core);
model.buildscripts = GetModelScripts(core);
model.bbMeshes = GetModelMeshes(core);
model.Rarity = RarityTypes.Common;
model.LifeTime = 9999;
model.IsFinished = false;
model.IsRigid = false;
model.Description = "needs description";
model.blueprint = new Blueprint();
ModelItem modelItem = ItemGenerator.CreateModelItem(model.Name);
model.modelItem = ItemGenerator.CreateModelItem(model.Name);
CreateNewModelPrefab(core, "Assets/Resources/Models/" +model.Name +".prefab");
ModelList.Add(model);
ModelObjList.Add(model.Core);
ModelNameList.Add(model.Name);
modelcount++;
return model;
}
static void CreateNewModelPrefab(GameObject obj, string localPath) {
Object prefab = PrefabUtility.CreateEmptyPrefab(localPath);
PrefabUtility.ReplacePrefab(obj, prefab, ReplacePrefabOptions.ConnectToPrefab);
}
`
Answer by aldonaletto · Dec 19, 2012 at 04:22 AM
Are you using this script in the Editor or at runtime? As far as I know, prefabs are Editor stuff, and you can't create them at runtime. If you want to let the player construct "prefab-like" objects at runtime, simply declare the "prefab" game object inactive to make it disappear of the scene, and use it as a regular prefab in Instantiate (remember to activate the object when instantiated)
I am using this at runtime, it creates the prefab fine. its just that the gameobjects all loose their meshes when its created
PrefabUtility is definitely an editor-only class, but I think ghostboxer means he is procedurally creating prefabs at editor runtime, not built game runtime.
If you're creating prefabs at game runtime, you'll certainly have problems like this one - prefabs are intended to be created in the Editor, not during the game. But you don't need to create a real prefab (unless you need to save it to the disk): get a reference to the object assembled by the user and use it as a prefab (in Instantiate, for instance) - just make sure the original object isn't destroyed during the game. You can move the original object to far far away, so that it's not visible and can't interfere with the other scene objects.
Yes, you can't use this class at runtime. You can use it in the editor, but when using it in a runtime script you can't build your game. Just try it, the compiler will complain that the class doesn't exist.
Furthermore your "mesh problem" is quite simple. You use a mesh merger script which creates a mesh on-the-fly in memory. Prefabs (which are an editor only feature) have to completely consist of assets. Assets have to be stored in the projects AssetDatabase.
If you want to turn a gameobject into a prefab (as an editor feature since at runtime this won't work as all), you have to store the combined $$anonymous$$esh as asset and use the asset as shared$$anonymous$$esh.
Answer by ghostboxer · Dec 20, 2012 at 08:06 PM
Thank you guys, I switched to creating a game object off scene and deactivating it, then I instantiate it where ever needed, and reactivate it.
Your answer
Follow this Question
Related Questions
using WWW to upload prefabs/game objects in webplayer... help 1 Answer
Instantiate object 1 Answer
How can record the total amount of prefabs that are spawned during runtime? 0 Answers
Why do Instantiated GameObjects Colliders only work on player i am controlling,nothing else? 2 Answers
NaN exception when spawning soldiers 1 Answer