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
0
Question by ArcadeRoyal · Mar 29, 2018 at 05:33 PM · materialsrenderer

Changing Color of Secondary Material in Materials Array

Hi Friends,

I'm having trouble doing programmatic updates to materials when I've got multiple materials on a mesh. I am consistently able to update the material in the [0] position of the materials array, but I'm not able to get the material in materials[1] to update regardless of what I try.

I've got a mesh with two section that get two different materials, which looks more or less like this:
alt text
The definition of this mesh is like this:
alt text

If I execute the following code in a script's Start() function, the color of the metal material turns red properly at the start of runtime, but the glow material stays yellow:

 GetComponent<Renderer>().materials[0].color = Color.red; 
 GetComponent<Renderer>().materials[1].color = Color.red; 


The upvoted answer from this other question (https://answers.unity.com/questions/124794/how-to-replace-materials-in-the-materials-array.html - this is from 2011, so likely things have changes since then) suggests that the materials in the materials array are copies, and so the solution is to replace the entire array, which I've tried doing, but the end-result is identical - only the material in position [0] changes, the material in position [1] is unchanged.

 Material[] temp = GetComponent<Renderer>().materials;
 temp[0].color = Color.red;
 temp[1].color = Color.red;
 GetComponent<Renderer>().materials = temp;  


I've been scratching my head at this one, and will continue checking, but has anyone else run into this?

capture.png (113.4 kB)
capture.png (32.6 kB)
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

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

Answer by Martin_Gonzalez · Mar 29, 2018 at 07:16 PM

I've tried this. Just to be sure that there is no other issues between.

  • Create a Cube in the scene

  • Create a material in your project tab

  • To the Cube material array in Renderer component add the created material

  • Add the following script into the cube and hit play and check if the material chage it color.


     using UnityEngine;
     
       public class MaterialTest : MonoBehaviour {
           private void Start() {
             var materials  = GetComponent<Renderer>().materials;
             foreach (var material in materials) {
                 Debug.Log(material);
             }
             
             materials[1].color = Color.red;
         }
     }
    
    
    

It worked for me

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 ArcadeRoyal · Mar 30, 2018 at 09:30 PM 0
Share

Sorry for the delay in getting back on this; this worked for me, and while running this example code I discovered that the issue with my original test-cases was that Unity wasn't re-compiling the C# script I was editing, so even though I was changing the C# file the actual class was remaining static to a version that changed only the first material.

After testing properly, simply calling "GetComponent().materials[1].color = Color.red" directly works as expected, so all issues were co$$anonymous$$g from my script not re-compiling. I fixed that in my unity project, so we're all good now. Thanks so much!

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

79 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 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 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

Change the materials of a list of game objects to one material, and then change it back to their old ones. 1 Answer

Combine materials out of renderer.materials 1 Answer

Same texture across two materials, is the texture loaded twice? 0 Answers

Multiple Materials on Particle System? 1 Answer

Changing two different objects renderer colour 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