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 · materialruntimerenderermeshrenderer

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

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

Answer by ops8086 · Oct 27, 2011 at 06:00 PM

I solved the problem (as suggested by another user in another question (the same as this) that i've opened for error):

Doing this way it works:

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

Thanks :)

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
0

Answer by BalsamicVinegar · Oct 27, 2011 at 12:45 AM

That should work fine. Perhaps it's where you are calling it? Are you sure you aren't overwriting it again later on?

try gameObject.renderer.materials[1] = theNewMaterial;

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 perchik · Feb 14, 2014 at 08:28 PM 2
Share

You'd think that should work (and nothing in the documentation suggests it wouldn't) but it doesn't work. I think the elements of the array must be read-only, but the array itself isn't which is why @ops8086's solution is correct. I just ran into this problem and changing the whole array was the only way to get it to work

avatar image Binxalot perchik · Nov 22, 2017 at 06:07 PM 0
Share

I think you're right on that. I was driving myself crazy on this using the gameObject.getComponent().materials[1] = theNew$$anonymous$$aterial; and like you said it definitely must be read only because your solution is the only thing that works, $$anonymous$$aybe its a bug or a missing point in the documentation but as of Unity 2017 this is still the way it has to be done.

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

6 People are following this question.

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 ? 0 Answers

Changing two different objects renderer colour 1 Answer

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

Can I access the Material of an animated object via code? 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