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 Gekigengar · Nov 21, 2013 at 10:04 AM · materialcolorchildchildrengroup

How to change color to every specific material in a child of a group..?

I got this code from somewhere else.

 foreach(Renderer r in target.parent.GetComponentsInChildren()) {
         foreach(Material M in r.materials) {
         M.color = setColor;
         }
 }

How do I make it so it only changes color to a certain material?

The material name is "Primary Color", How do I refer to them..? can I use string? or should I use an Index?.

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

Answer by GameVortex · Nov 21, 2013 at 11:05 AM

If your script is on a gameobject, you could just make a material property and drag in the material reference in the inspector and then check in your loop if the material in the renderer is the same material.

Example:

 public Material material;
 
 public void YourFunction() {
     foreach(Renderer r in target.parent.GetComponentsInChildren()) {
             foreach(Material M in r.materials) {
                 if(M == material) {
                     M.color = setColor;
                 }
             }
     }
 }

Edit: Actually, if they use the same material, you only need to change the color of the reference:

 public Material material;
 
 public void YourFunction() {
     material.color = setColor;
 }


Now, if they don't use the exact same material and rather its own material instance based on the same material; then I would check for the name of the material instead.

Comment
Add comment · Show 4 · 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 Gekigengar · Nov 21, 2013 at 11:26 AM 0
Share

The first method and the second did not work.

Actually, the material are instanced, since Every player would have a different coloring. (Object group of my tank for example, which uses multiple material, and each player will have different $$anonymous$$m coloring.)

How do I check for the name? Wouldn't doing that will also change other player's instance since their name is "$$anonymous$$aterialX (Instance)" too?.

avatar image GameVortex · Nov 21, 2013 at 11:33 AM 0
Share

Well, you specify to only change color of materials of the target, so as long as the target variable is only one player, you only change color of that player.

 foreach(Renderer r in target.parent.GetComponentsInChildren() {
     foreach($$anonymous$$aterial $$anonymous$$ in r.materials) {
         if($$anonymous$$.name == nameToCheck) {
             $$anonymous$$.color = setColor;
         }
     }
 }
avatar image Gekigengar · Nov 21, 2013 at 12:46 PM 0
Share

It works now!, Using the string comparison.

I need to add material.name + " (Instance)" to get the stuff working :D Thank you! $$anonymous$$ay you be blessed good sir~!

avatar image GameVortex · Nov 21, 2013 at 01:29 PM 0
Share

Glad you figured it out =)

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

18 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

Related Questions

FindChild/Change color 1 Answer

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

Changing two different objects renderer colour 1 Answer

Change the color of a child 1 Answer

Change material of all the children 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