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
1
Question by hawken-2 · Mar 10, 2012 at 10:18 AM · materialloopchildrenfor

change material in children

I have a GameObject which has no renderer it's self, rather everything is a child, they all share the same material. Instead of listing vars for each child in the GameObject, I would rather loop a for that runs through the children and applies the correct material when that prefab is instantiated. Thing is, documentation on this exact process is thin on the ground. How does a for loop work in this case??

 var frog : GameObject;
     var randomMatNum : int = Random.Range(1, 5);
     var randomMat : Material;
     var greenFrog : Material;
     var blueFrog : Material;
     var pinkFrog : Material;
     var yellowFrog : Material;
     
     if (randomMatNum == 1) randomMat = greenFrog;
     else if (randomMatNum == 2) randomMat = blueFrog;
     else if (randomMatNum == 3) randomMat = pinkFrog;
     else if (randomMatNum == 4) randomMat = yellowFrog;
     
     
     
     for (var frogChild in frog.GetComponentInChildren(Renderer))
     {
         frogChild.material = randomMat;
     }

Get an error, not sure how this is done TBH.

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

3 Replies

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

Answer by Kleptomaniac · Mar 10, 2012 at 10:52 AM

Hey there! Don't worry you're almost there.

In terms of the for loop, you're close: you shouldn't need to use a GetComponentInChildren to access their renderers, using .render.material shoul work just fine. As for accessing the children themselves, you would use for (var frogChild : Transform in transform) {, accessing any children (Transforms) within the object the script is attached to.

Also, I suggest instead of using separate variables for each material, use a Material[] array. That way you could assign all your different materials to a single var in the inspector, and then use your Random.Range to return the element of that number.

All in all, your script should end up looking like this:

 var randomMatNum : int = Random.Range(1, 5);
 var randomMat : Material[];
 
 function Update () {
 
     for (var frogChild: Transform in transform) {
         frogChild.renderer.material = randomMat[randomMatNum];
     }
 }

Have fun with your game! Klep

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 hawken-2 · Mar 10, 2012 at 11:13 AM 0
Share

Thanks! This did the trick, I'll post the code I used below:

avatar image Kleptomaniac · Mar 10, 2012 at 11:22 AM 0
Share

Sweet! No worries, happy to help!

avatar image
0

Answer by Paulius-Liekis · Mar 10, 2012 at 10:41 AM

What error do you get? Your loop has GetComponentInChildren instead of GetComponentsInChildren (plural).

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 hawken-2 · Mar 10, 2012 at 11:13 AM

 var randomFrogMat : Material[];
 
 function Start() {
     var randomMatNum : int = Random.Range(0, 4);
     for (var frogChild: Transform in transform) {
            frogChild.renderer.material = randomFrogMat[randomMatNum];
     }
 }
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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Change material of all the children 2 Answers

list.contains isn`t working 1 Answer

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

Has anyone ever used code to change the materials on an instantiated game object? 1 Answer

retrieving data from SQLite to UNITY 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