Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by ghostboxer · Dec 19, 2012 at 03:55 AM · gameobjectmodelprefabsruntime

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);
	}
	`

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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)

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ghostboxer · Dec 19, 2012 at 04:37 AM 0
Share

I am using this at runtime, it creates the prefab fine. its just that the gameobjects all loose their meshes when its created

avatar image yoyo · Dec 19, 2012 at 04:44 AM 1
Share

PrefabUtility is definitely an editor-only class, but I think ghostboxer means he is procedurally creating prefabs at editor runtime, not built game runtime.

avatar image aldonaletto · Dec 20, 2012 at 12:41 AM 1
Share

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.

avatar image Bunny83 · Dec 20, 2012 at 12:50 AM 1
Share

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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges