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
0
Question by ThiagoTejo · Oct 27, 2013 at 04:52 PM · materialsrenderercolors

Change color to multiple Materials in one gameObject?

Hello guys. I'm trying to change the color for all materials on my gameObject, but it only change the first one...

I'm using this code:

 gameObject.renderer.material.color = Color.red;
   

but the gameObject i want to change the color, have tons of materials :( is there a way to change the colors to all materials easily?

Comment
Add comment · Show 3
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 ThiagoTejo · Oct 27, 2013 at 05:30 PM 0
Share

It says it need an ")" after mat, why? :(

avatar image ThiagoTejo · Oct 27, 2013 at 10:35 PM 0
Share

Thanks! But how do i make it return to the original color? i tried for (var i = 0; i < renderer.materials.length; i++) { renderer.materials[i].color = Color.red; } yield WaitForSeconds (0.6); renderer.materials[i].color = Color.white;

but it gives me IndexOutOfRange... Actually, it would be better if i could return to the color before the red, because not every material is white. Ty ^^

avatar image tanoshimi · Oct 27, 2013 at 10:56 PM 0
Share

Your yield and second materials assignment statement are not contained in the for loop currently - move the closing curly bracket to the end of that snippet. To return to the original material, you'll have to create a material[] array and populate it with the starting materials, then loop through the array and reassign them.

3 Replies

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

Answer by lachesis · Oct 27, 2013 at 10:53 PM

Unfortunately, materials doesn't have a .color method, as .color addresses the main material on an object, while materials returns the full array of materials on an object. If you'd like to modify the color of all materials on an object, better to cache the original materials, then modify the array inside of a for loop.

 oldMaterials : Material[] = renderer.materials;
 
     for (var i = 0; i < renderer.materials.length; i++) {
         renderer.materials[i].color = Color.red;
     }

Then, once you've completed any other operation you'd like, just restore your old material array:

 renderer.materials = oldMaterials;
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 ThiagoTejo · Oct 27, 2013 at 11:45 PM 0
Share

Thank you! But it still doesn't return to the old materials... I've done exactly like you said.

 var old$$anonymous$$aterials : $$anonymous$$aterial[] ;
 
 function Start ()
 {
 children = GameObject.Find("AdultLink");
 old$$anonymous$$aterials = children.renderer.materials;
 }
 
 function Hit ()
 {
   for (var i = 0; i < children.renderer.materials.length; i++) {
         children.renderer.materials[i].color = Color.red;
     }
 
 yield WaitForSeconds (0.6);
 
 children.renderer.materials = old$$anonymous$$aterials;
 }

It just turns red, but doesn't go back to old$$anonymous$$aterials after the WaitForSeconds...

avatar image ThiagoTejo · Oct 28, 2013 at 11:42 AM 0
Share

I got it now, the problem is, that, when i change the color to red, it also change the colors to red on old$$anonymous$$aterials... why?? another strange thing, is when i store the children materials into old$$anonymous$$aterials, it automaticaly changes the materials for my children gameObject! it changes to "nameofmaterial(Instance)", the same one that gets stored into old$$anonymous$$aterials, why? :(

avatar image Tomer-Barkan · Oct 28, 2013 at 11:54 AM 1
Share

Because they both reference the same material, it won't work. Once you change the material to red, it will also change the one you stored, since it's just a reference.

You'll have to store an array of colors ins$$anonymous$$d.

avatar image ThiagoTejo · Oct 28, 2013 at 12:28 PM 0
Share

And how can i store an array of colors from the children materials? i tried a few things here, but none work... and after that, how can i go back to it?

avatar image vexe · Oct 28, 2013 at 12:38 PM 1
Share

@tbkn is correct. $$anonymous$$aterial is a reference type, Color is a value-type (struct) - So when you assign colors, you're getting back copies, and not references. So you could just do:

 var oldColors : Color[];
 
 function Awake()
 {
    oldColors = new Color[renderer.materials.Length];
 }
 
 function CacheColors()
 {
    for(var i = 0, len = renderer.materials.Length; i < len; i++)
       oldColors[i] = renderer.materials[i].color;
 }
 
 function ResetColors()
 {
    for(var i = 0, len = renderer.materials.Length; i < len; i++)
       renderer.materials[i].color = oldColors[i];
 }
avatar image
2

Answer by Tomer-Barkan · Oct 27, 2013 at 04:55 PM

use renderer.materials instead of renderer.material:

C#:

 foreach (Material mat in renderer.materials) {
     mat.color = Color.red;
 }

Javascript:

 for (var i = 0; i < renderer.materials.length; i++) {
     renderer.materials[i].color = Color.red;
 }

If you want to return the original colors, you have to first change ALL the colors, then yield, then after returning change back ALL the colors. You can't yield between changing one material and changing it back...

 for (var i = 0; i < renderer.materials.length; i++) {
     renderer.materials[i].color = Color.red;
 }

 yield WaitForSeconds(0.6);

 for (var i = 0; i < renderer.materials.length; i++) {
     renderer.materials[i].color = Color.white;
 }
 
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 marpione · Apr 10, 2018 at 11:57 AM

You can also use material Property blocks which are faster than creating materials at runtime.

Declare a MPB and init it later use it in a for loop. In 2018 you can give it a material index when setting the meshrenderer MPB which in this case it's "i".

 MaterialPropertyBlock MPB;
 
 void ChangeMaterials()
 {
 MPB = new MaterialPropertyBlock();
 
 for(i = 0; i < meshRenderer.materials.coun; i++)
 {
 MPB.SetColor(Color.Red)
 
 meshRenderer.SetPropertyBlock(MPB, i);
 }
 }
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

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

20 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

Related Questions

Changing two different objects renderer colour 1 Answer

Emissive Material control via script (code) 1 Answer

Changing Color of Secondary Material in Materials Array 1 Answer

My foreach or the Resources.LoadAll Is not getting the information I need 2 Answers

Looping on materials.. 2 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