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 /
This question was closed Jul 07, 2014 at 04:29 AM by QuestionAsker for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by QuestionAsker · Jul 06, 2014 at 10:16 PM · meshprefabmaterialduplicatecopy

Possible Unity Glitch. A few lines of code erasing prefab data.

I was assuming that meshes and materials acted like everything else and using the '=' operator would duplicate it:

  this.GetComponent<MeshFilter>().mesh = constructionPhases[0].GetComponent<MeshFilter>().mesh;
  this.renderer.materials = constructionPhases[0].renderer.materials;

And it erased my mesh and materials on my prefab! So, I remade the prefabs and changed the code to:

 this.GetComponent<MeshFilter>().mesh = (Mesh) Instantiate(constructionPhases[0].GetComponent<MeshFilter>().mesh);
             
 List<Material> ms = new List<Material>();
 foreach(Material m in constructionPhases[0].GetComponent<MeshRenderer>().materials)
         ms.Add((Material) Instantiate(m));
 this.GetComponent<MeshRenderer>().materials = ms.ToArray();

And again it erased the prefab component data! I can not keep remaking the prefabs. I need to know how to correctly duplicate a mesh and its materials. Thank you in advance!

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

  • Sort: 
avatar image
0
Best Answer

Answer by Radivarig · Jul 07, 2014 at 12:22 AM

In your case '=' operator passes a reference instead of duplicating it. When reference is passed you have to use constructor of the class you want to duplicate.

Try this

 copiedMaterial = new Material(oldMaterialHolder.material);



Comment
Add comment · Show 17 · 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 QuestionAsker · Jul 07, 2014 at 02:37 AM 0
Share

That didnt work for the mesh :/ It says $$anonymous$$esh doenst have a constructor with one argument.

avatar image Radivarig · Jul 07, 2014 at 02:49 AM 0
Share

You will have to duplicate each part of it and then store in a new $$anonymous$$esh(), look at this forum thread and try the scripts below, these guys were doing exactly what you need.

avatar image QuestionAsker · Jul 07, 2014 at 03:02 AM 0
Share

Alright well I used the script and it still changes the $$anonymous$$eshFilter.mesh to a 'Type$$anonymous$$ismatch' which then erases it. Also, the way suggested to duplicate materials did not work for me. It sill erases them :/ This is unnecessarily complicated.

avatar image Radivarig · Jul 07, 2014 at 03:11 AM 0
Share

Are you doing it in edit or play mode?

avatar image QuestionAsker · Jul 07, 2014 at 03:12 AM 0
Share

Play. The code runs when i hit a button onscreen

Show more comments

Follow this Question

Answers Answers and Comments

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can you duplicat folders and not share old dependencies? 1 Answer

What process should I best use to properly create an updated copy of a Prefab? 1 Answer

How should I structure this in Unity? 1 Answer

Duplicate a Material? (Project Window) 1 Answer

Prefab Materials and Meshes being deleted, How can I stop this 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