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 /
  • Help Room /
avatar image
0
Question by RiQQ92 · Apr 19, 2016 at 02:42 PM · scripting problemmaterialreferencematerial renderer

Help, cannot change reference of Renderer.material

I have fought with this for some time and done lots of googling yet i have failed to succeed in this. In my game the player can create alot of objects at runtime and i want him to be able to change what color the created objects are. Some could be blue, some red, etc. and i had it working in a way that it always created new instance of material, which later on now caused me quite a bottleneck on dynamic batching.

I have created a material/color storage class system and am able to save them there succesfully but i can't apply them to the objects Renderer.sharedMaterial for some reason (it doesn't change anything) and if i use Renderer.material it creates new instance. Most likely the problem here is my lack of understanding how the materials/renderer works. Any help appreciated.

Heres the code im running:

 private void CreateObject()
     {
         if (!GetInput.cursorOverHud)
         {
             foreach (Transform t in Placeholders)
             {
                 Transform o = UnityEngine.Object.Instantiate(t, t.position, t.rotation) as Transform;
 
                 foreach (Collider col in o.GetComponents<Collider>())
                     col.isTrigger = false;
 
                 o.gameObject.layer = 9;
                 o.GetComponent<Rigidbody>().isKinematic = false;
                 o.GetComponent<DynamicObject>().Set();
                 Renderer oRend = o.GetComponent<Renderer>();
                 Renderer tRend = t.GetComponent<Renderer>();
 
                 for (int i = 0; i < oRend.sharedMaterials.Length; i++)
                 {    
                     // Nothing happens
                     oRend.sharedMaterials[i] = MatColStorage.GetMaterial(tRend, tRend.sharedMaterials[i].color, i);//mat[i];//tRend.sharedMaterials[i];
                     // But if i were to assign into 'oRend.materials[i]' i get lots of instantiated materials
                 }
             }
         }
     }

And the storage class:

 public static class MatColStorage
 { 
     private class MaterialColor
     {
         private Material mat;
         private Color col;
 
         public Material material
         {
             get
             {
                 return mat;
             }
         }
         public Color color
         {
             get
             {
                 return col;
             }
         }
 
         public MaterialColor(Material material, Color color)
         {
             mat = material;
             mat.color = color;
             col = color;
         }
     }
     private static List<MaterialColor> instantiatedMaterials = new List<MaterialColor>();
 
     public static Material GetMaterial(Renderer rend, Color col, int whichMat)
     {
         foreach(MaterialColor matcol in instantiatedMaterials)
         {
             if(matcol.color == col)
             {
                 return matcol.material;
             }
         }
 
         Material refMat = new Material(rend.materials[whichMat]);
         refMat.color = col;
         instantiatedMaterials.Add(new MaterialColor(refMat, col));
         
         return instantiatedMaterials[instantiatedMaterials.Count - 1].material;
     }
 }
Comment
Add comment · Show 1
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 RiQQ92 · Apr 20, 2016 at 06:18 AM 0
Share

bump. Any ideas, insight, anything?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Blue-Cut · Apr 22, 2016 at 10:02 PM

Hello,

This is not going to work :

 oRend.sharedMaterials[i] = MatColStorage.GetMaterial(tRend, tRend.sharedMaterials[i].color, i);

Unfortunatly, I don't have the knowledge to explain you in detail why it's not working, but the fact is the the array sharedMaterials[] behaves like the properties you can't access directly. It's the same kind of not working case than :

 // This is not working, you need to use a tmp vector
 transform.position.x = myFloat;

So what you need to do is :

 Material[] tmpMats = new Material[];
         
 // you have your for loop here
 tmpMats[i] = MatColStorage.GetMaterial(tRend, tRend.sharedMaterials[i].color, i);
             
 oRend.sharedMaterials = tmpMats;
 
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 RiQQ92 · Apr 22, 2016 at 11:04 PM 0
Share

Whoa thank you so much! Darned debugger never even noted me about the fact that i cant do it, i even checked the UnityEngine.dll and i completely missed that it's an property.

Thank you again, horrible problem it was.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

MonoDevelop Unity API reference function with wrong path. How to Fix it? 1 Answer

Do I need a thousand materials or not?,, 0 Answers

Transparent texture color? 0 Answers

Script to get Eye Material to move only seems to work in Inspector/Edit Mode 0 Answers

How to Fix this issue 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