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 /
This question was closed Oct 03, 2011 at 05:08 PM by DavidDebnar for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by DavidDebnar · Jun 28, 2011 at 03:26 PM · materialchildloopchangechildren

Change material of all the children

Hey guys, I know, that this is probably pretty easy, but I wanna code it fast and my brain is kinda slow tonight. So, I have an object, that has around 10 children and a few of them have other children and a few of the other children of the children have more children. Simply, it goes like 4 levels downwards. I made this

var mat : Material;

function Start () { for (var child : Transform in transform) { child.renderer.material = mat; } }

but it only changes the material of the first level children. I hope that you got what I want :)

  • David

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

  • Sort: 
avatar image
3
Best Answer

Answer by flaviusxvii · Jun 28, 2011 at 03:45 PM

This is sort of pseudo-codish.. but it's the right idea.. You need to recurse.

 function setMaterial(go : Transform, mat : Material)
 {
     for(var child : Transform in go) {
          child.renderer.material = mat;
          setMaterial(child, mat); // This will repeat the process on the child.
     }
 }

Then call setMaterial on your top level transform with the material you want.

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 AM_1 · Jul 31, 2014 at 04:41 AM 0
Share

I think this code would be better like this:

 function set$$anonymous$$aterial(go : Transform, mat : $$anonymous$$aterial)
 {
     for(var child : Transform in go) {
         set$$anonymous$$aterial(child, mat); // This will repeat the process on the child.
         child.renderer.material = mat;
     }
 }

Your way, the material is set twice, in the main method and in the branch. This way, materials are only set once after the code has recursed to the deepest level.

avatar image
0

Answer by Tommy · Jun 28, 2011 at 03:43 PM

Simple solution would be to just attach the script to the childs. Maybe.

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

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Changing texture of childrens 2 Answers

FindChild/Change color 1 Answer

Change the color of a child 1 Answer

Can't get children/Color not part of material 1 Answer

Loop to find a child in every children's child? 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