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 csgeorge · Jun 26, 2016 at 08:36 PM · c#collisionmaterialschild

Change Material on Child Objects Upon Collision

I'm trying to change the material on a game object and its children when it receives a collision. With the current setup I have (below), the game object's material changes successfully but the children's do not. Can anyone tell me what needs to be changed or fixes I might try?

 public Material demagedMaterial;
 public Renderer childColor;
 
 void Start() 
 {
     childColor = GetComponentInChildren<MeshRenderer>();
 }
 
 void OnCollisionEnter(Collision collision)
 {
     gameObject.GetComponent<MeshRenderer>().material = demagedMaterial;
     childColor.material = demagedMaterial;
 }
Comment
Add comment · Show 2
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 Mindmapfreak · Jun 26, 2016 at 09:51 PM 0
Share

Do you have multiple children? GetComponentInChildren only gets the first (depth first search) component, you should use GetComponentsInChildren (note the additional "s") and iterate through them to set the material for all children.

avatar image csgeorge Mindmapfreak · Jun 28, 2016 at 01:17 AM 0
Share

I changed it to GetComponentsInChildren and it actually produced this error:

error CS0029: Cannot implicitly convert type UnityEngine.$$anonymous$$eshRenderer[]' to UnityEngine.Renderer'

(I got a similar one when I changed the variable to be a $$anonymous$$eshRenderer type as well, so doesn't seem like that's the issue.)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by wesleywh · Jun 28, 2016 at 04:32 AM

That error is the difference between

Getting first found child...

 childColor = GetComponentInChildren<MeshRenderer>();

...and getting all found children

 childColors = GetComponentsInChildren<MeshRenderer>();

This is what your code should be changed to:

 public Material demagedMaterial;
  public Renderer[] childColors;
  
  void Start() 
  {
      childColors = GetComponentInChildren<MeshRenderer>();
  }
  
  void OnCollisionEnter(Collision collision)
  {
      gameObject.GetComponent<MeshRenderer>().material = demagedMaterial;
      foreach(Renderer color in childColors) {
           color.material = demagedMaterial;
      }
  }

Note that the following lines were changed:

 public Renderer childColor;

 childColor = GetComponentInChildren<MeshRenderer>();

 childColor.material = demagedMaterial;

Hopefully this helps.

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 csgeorge · Jul 02, 2016 at 08:22 PM 0
Share

Hi- thanks for the advice, but this produced the same error I mentioned on the post above.

avatar image Asgardr csgeorge · Jul 02, 2016 at 08:27 PM 0
Share

That's because he made a small mistake. The variable definition should be:

 public $$anonymous$$eshRenderer[] childColors;

not

 public Renderer[] childColors;
avatar image
0

Answer by EDevJogos · Jul 02, 2016 at 09:32 PM

Why do you do:

 void Start() 
  {
      childColor = GetComponentInChildren<MeshRenderer>();
  }

childColor is not a direct reference to the child renderer ? if it is you are trying to get the material from a null object, if it is not, make one, GetComponentInChildren is slow, way better to have a direct reference to the object if you can.

Also use Renderer instead of MeshRenderer.

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

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

Trail prefab as the child of existing object. 1 Answer

collision on child only returns parent object ? 1 Answer

C# Ignore Collision Trigger From Child 2 Answers

Distribute terrain in zones 3 Answers

Attaching objects on collision 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