• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by axonJeff · Dec 03, 2012 at 05:49 PM · c#meshprefabcreate

Mesh and material disappear after CreatePrefab() in C#

I'm currently working on a model importing tool in C# for the Unity Editor, which runs OnPostProcessAllAssets(), reads each model file and is supposed to create a prefab for each one containing the Mesh that was constructed from the model file data.

I have gone about doing this by creating an empty GameObject for each model, and then I assign the Mesh to the GameObject's MeshFilter.mesh. It's clear to me that this works, since I can see the applied Mesh in the Scene view.

However, the problem arises when this GameObject is converted into a prefab:

The prefab does not contain a Mesh! It has a MeshFilter component, but the Mesh comes up as "None (Mesh)". Furthermore, the material/shader that I assign to the MeshRenderer also disappears.

The creation of the GameObject in the Scene goes like this:

 Mesh importedMesh = null;
 importedMesh = ImportModelFromXml(asset);
 
 string prefabName = fileName.Remove( fileName.LastIndexOf(MODEL_SUFFIX) );
 
 GameObject go = new GameObject(prefabName);
 go.AddComponent<MeshRenderer>().material = new Material( Shader.Find("Transparent/Diffuse") );
 go.AddComponent<MeshFilter>().mesh = importedMesh;

And ImportMexhFromXml(string asset) looks like this (it's not reading from a file yet -- just creating a unit tetrahedron as a test):

 static Mesh ImportModelFromXml(string filePath)
 {
     Debug.Log("Reading .XML model: " + filePath);

     #region Read through the .XML format and assign all of the mesh data to a single mesh
 
     Vector3 p0 = new Vector3(0f, 0f, 0f);
     Vector3 p1 = new Vector3(1f, 0f, 0f);
     Vector3 p2 = new Vector3(0.5f, 0, Mathf.Sqrt(0.75f) );
     Vector3 p3 = new Vector3(0.5f, Mathf.Sqrt(0.75f), Mathf.Sqrt(0.75f) / 3) ;
 
     Mesh importedMesh = new Mesh();
 
     importedMesh.vertices = new Vector3[] {
         p0, p1, p2,
         p0, p2, p3,
         p2, p1, p3,
         p0, p3, p1
     };
     importedMesh.triangles = new int[] {
         0,  1,  2,
         3,  4,  5,
         6,  7,  8,
         9, 10, 11
     };
 
     importedMesh.RecalculateNormals();
     importedMesh.RecalculateBounds();
     importedMesh.Optimize();
 
     #endregion
 
     Debug.Log(".XML Mesh import complete!");
 
     // Return the Mesh to the caller
     return importedMesh;
 }

I am using the following statement to create the prefab from the GameObject in the Scene:

 PrefabUtility.CreatePrefab(prefabPath, go, ReplacePrefabOptions.ReplaceNameBased);

Could someone possibly explain why my Mesh is disappearing when I make this GameObject into a prefab? Do I have to do something special to bring the component contents over to the prefab?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Paulius-Liekis · Dec 04, 2012 at 11:30 AM

I think this is what's happening: your material and mesh are created as plain C# objects. Once you assign them to the GameObject they are linked and saved to Scene file. When you create a Prefab it copies your object to Project, but it does not do the same for your Mesh and Material, i.e. a project-level object can not reference scene-level objects. You have to understand this boundary between project and scene objects.

The solution: store Mesh and Material in your prefab. I don't remember function exactly, but there is something like add-asset-to-prefab or add-object-to-asset.

Comment
Add comment · Show 1 · 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 axonJeff · Dec 04, 2012 at 04:00 PM 0
Share

I understand your concern here, which is why I was suspicious that maybe something else needed to be done to map the mesh to the prefab. However, seeing as I'm able to see the GameObject with the material and mesh applied in the scene it was my understanding that they were scene objects. Unity seems to be treating them as such.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

How do I initialize a simple light in C#? 2 Answers

Is it possible to make thousands of colliders at once? 1 Answer

Few problems with arrays 1 Answer

Accessing a game object from a script? 1 Answer

How to access prefab variables before instantiating? 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