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
3
Question by ops8086 · Oct 26, 2011 at 10:44 PM · materialmaterialsruntimemeshrenderer

How to change a specific material of an object ?

Hi saw that this question has posted many times, but all the times the answer is to use renderer.material = theNewMaterial;

The problem is that if the current object has more than one material in the Renderer, how can i change a specific material ? For example, i have an object whose mesh is composed by 4 materials, so in the renderer i have material[0], material[1], and so on. What i've to do if i want to assign (at runtime) another material to the material at index 1, for example ?

Doing this way doesn't work:

renderer.materials[1] = theNewMaterial;

I hope someone can help me.

Thanks!

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 roamcel · Oct 27, 2011 at 09:48 AM 1
Share

Are you sure it doesn't work? The new material is a new material instance, or a material reference from a prefab? Check out the inspector for the object to be sure.

1 Reply

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

Answer by aldonaletto · Oct 27, 2011 at 11:59 AM

According to the docs, Renderer.materials is just a copy of the internal array - that's why modifying one of its elements doesn't work. You should copy the materials to a temp array, modify the element and store the temp array in materials:

     var mats = renderer.materials;
     mats[1] = theNewMaterial;
     renderer.materials = mats;


Comment
Add comment · Show 5 · 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 ops8086 · Oct 27, 2011 at 05:58 PM 0
Share

Thank you! it works! :)

avatar image Slandapanda · Feb 27, 2013 at 02:51 PM 1
Share

Just in case anyone stumbles across this whilst looking for C# instructions like I did, this worked for me:

 $$anonymous$$aterial[] mats;
 mats = renderer.materials;
 mats[1] = theNew$$anonymous$$aterial
 renderer.materials = mats;
avatar image ptblk · Jul 20, 2015 at 04:43 PM 0
Share

hi, noob coder, does this go under void update? Confused as how to finish the code off as it seems to only be a part of it. Thanks

avatar image Slandapanda · Jul 23, 2015 at 12:33 PM 0
Share

In C#...

 $$anonymous$$aterial[] mats;

This ^ goes below "public class yourScript : $$anonymous$$onoBehaviour {" and before void Update or any other functions.

  mats = renderer.materials;
  mats[1] = theNew$$anonymous$$aterial
  renderer.materials = mats;

These ^ go where ever you want to make the material to change.

Hope that makes sense.

Not sure about Javascript if that's what you were after.

avatar image Cookie042 Slandapanda · Aug 31, 2019 at 02:54 AM 0
Share

@Slandapanda the OP is using valid c#, nothing needs to be changed. caching it in the class doesnt really change anything either, still returns a new copy and GC cleans up the dereferenced array.

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

7 People are following this question.

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

Related Questions

Material doesn't have a color property '_Color' 4 Answers

How to change a specific material of an object at runtime ? 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Change specific material on an object with multiple materials?,Change specific material on an object in a script? 0 Answers

Changing material during runtime 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