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 Sondre-S · Feb 13, 2015 at 08:37 PM · materialrenderergetcomponentinchildren

Do something for each material in each renderer in children...

I have a car mesh with separate parts but with same material... I'm trying to change color of all materials called "carMaterial" in all renderers in the child objects of the car object...

I have tried different approaches but with no luck...

 CarBodyRenderers= GetComponentsInChildren<Renderer>();
         foreach(Renderer rend in CarBodyRenderers){
                 foreach(Material matt in rend.materials){
         if(matt.name == "carMaterial"){
             etColor("_Color", carPaint);
                  }
               }
         }
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
1
Best Answer

Answer by Sondre-S · Feb 13, 2015 at 09:57 PM

Found my answer here: http://answers.unity3d.com/questions/205372/syntax-for-a-material.html

The correct syntax for this is:

 if(matt.name == "carMaterial (Instance)")

 

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 hexagonius · Feb 13, 2015 at 10:06 PM 0
Share

You're right, sorry. Components share the name with its GameObject, not Objects... stupid me

avatar image Sondre-S · Feb 13, 2015 at 10:17 PM 0
Share

Ok, I see:)

avatar image
0

Answer by hexagonius · Feb 13, 2015 at 09:21 PM

Grab the sharedMaterial instead of all materials of at least one object using it and modify the color. It should automatically change all of them.

Also comparing a materials name with a string actually checks against the GameObjects name (Material, Object name definition)

Comment
Add comment · Show 3 · 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 Sondre-S · Feb 13, 2015 at 09:42 PM 0
Share

that won't work, would'nt it change the color of all cars using the same material?

So, (matt.name == "car$$anonymous$$aterial") is not the right syntax? Then what is the correct syntax?

avatar image hexagonius · Feb 13, 2015 at 09:56 PM 0
Share

$$anonymous$$aybe you should create one material per car if it's not a model that's using one in total. That way you won't get your draw calls up. Doesn't matter with Sprite Renderers, there you would be right.

Just don't compare the names since matt.name refers to the car's name, not the material name

avatar image Sondre-S · Feb 13, 2015 at 09:58 PM 0
Share

Works now, I had to add (Instance) after the name. Btw I have a material for each car, but there may be more than one car of the same type, but with different colors. (For example a red Honda and a white Honda)

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

how do I change the properties of a material that are on the children of an object on script? 1 Answer

Changing specific materials onTriggerEnter 3 Answers

Are Mesh Filters Really Necessary? 1 Answer

Trying to make a side scroller. I'm following a tutorial and getting this error. 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