Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by jmgek · Dec 06, 2015 at 06:33 AM · editormaterialsdynamic

Generating new materials problem when making a prefab.

This is going to be hard to explain since I don't know the exact terminology.

I am making materials in the editor (dynamically) and applying them to generated mesh, but my problem comes when I want to turn my mesh into a prefab (with drag and drop into projects and with prefab utility), I get the "No pixel shader pink" when I try and see the prefab.

This was my code:

 obj.GetComponent<Renderer>().sharedMaterial = new Material(Shader.Find("SomeShaderIMade"));

I would like to keep that kind of format but I know you have to have some sort of material instanced, I have gotten it to work with using resources but it shares the material along all the objects and I need separate colors on them.

 obj.GetComponent<Renderer>().sharedMaterial =  Resources.Load("test") as Material;

Is there a way I can do this where I can bring in new materials and have my generated mesh produce correct prefabs?

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
0
Best Answer

Answer by jmgek · Dec 06, 2015 at 07:26 PM

I figured it out:

     int matcount = 0;
     private void ColorObject(GameObject obj)
     {
         matcount++;
         Material mat = obj.GetComponent<Renderer>().sharedMaterial = new Material(Shader.Find("MyShader"));
         AssetDatabase.CreateAsset(mat, string.Format("Assets/MyMaterial{0}.mat", matcount));
         obj.GetComponent<Renderer>().sharedMaterial.color = color;
     }

You need to create the material into the project so it has a dynamic link to a material with AssetDatabase. also if you go this way don't create a new material every time if it's the same color just share the material across all objects that share color.

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

Answer by kidmosey · Dec 06, 2015 at 08:32 AM

Resources.Load() will essentially generate a pointer to the prefab.

If you want to copy it, you can use Instantiate<Material>(loadedMaterial)

Also, using sharedMaterial could be an issue. You might need to use material instead.

Comment
Add comment · Show 2 · 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 jmgek · Dec 06, 2015 at 05:38 PM 0
Share

I understand what Resourse.load does but this is not what I am asking, shared material is because I am inheriting from the editor. and instancing still shares a material.

I appreciate the answer but in the future could you think about posting as a comment if you don't directly answer the question, others will see this as answered when it's not.

avatar image jmgek · Dec 06, 2015 at 06:59 PM 0
Share

I guess I have to assign materials in groups. Thanks for your help,

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

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

Related Questions

Pressing play Causes Re-instancing of material (Editor) 1 Answer

Enable Emission to update at run time in editor 5.6.1? 2 Answers

Assign gameobject icon dynamically 0 Answers

how to link variable as inspector field 1 Answer

Changing the materials in Editor Mode Without Getting the leak Error 3 Answers


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