Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
4
Question by TomatoOrgyLT · Mar 01, 2014 at 06:53 PM · arraymaterialchildrenelement

How to change all material elements of object's children

So my problem is that my game object changes half of its materials and the other half is not affected. I want to change all of it and I need to do it by accessing the object's children. Any help would be appreciated.

Here is what I did so far..

 var newMat : Material;
 
 function Start() {
 ChangeMaterial();
 }
 
 function ChangeMaterial() {
     var children : Renderer[];
     children = GetComponentsInChildren.<Renderer>();
         for (var i : Renderer in children) {
         i.material = newMat;
         }
 }
Comment
Add comment · Show 3
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 robertbu · Mar 01, 2014 at 07:06 PM 0
Share

Is this code on the parent object? Are all the children active?

avatar image TurboHermit · Mar 01, 2014 at 07:11 PM 0
Share

the only thing I could imagine is that he loop isn't finished the right way, in which case you should do a for loop with increments For example: for(var i = 0;i

avatar image TomatoOrgyLT · Mar 01, 2014 at 07:26 PM 0
Share

This code is on the parent object. I changed the for loop like your example and I get the same results.

3 Replies

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

Answer by robertbu · Mar 01, 2014 at 08:42 PM

Rereading your question, I'll bet your meshes have more than one material assigned. You will need to cycle through and reset all the materials on the mesh. Try this (untested):

     function ChangeMaterial(newMat : Material) {
     var children : Renderer[];
     children = GetComponentsInChildren.<Renderer>();
     for (var rend : Renderer in children) {
         var mats = new Material[rend.materials.Length];
         for (var j = 0; j < rend.materials.Length; j++) {
             mats[j] = newMat; 
         }
         rend.materials = mats;
     }    
 }
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 TomatoOrgyLT · Mar 01, 2014 at 09:28 PM 1
Share

Yes my mesh has more than one material assigned. I think I did reset the materials on the mesh and modified the script according to your example but it doesn't seem to change anything.

avatar image robertbu · Mar 01, 2014 at 09:34 PM 0
Share

I edited my answer. Apparently you cannot replace individually replace materials in the 'materials' array. You have to create a new array and replace the whole thing. Try the edited code.

avatar image TomatoOrgyLT · Mar 01, 2014 at 09:58 PM 0
Share

Yay! It works great! Thanks for the help.

avatar image gsmetzer · Aug 22, 2017 at 10:09 PM 0
Share

Sorry to bring this old answer up but how would you write this in C#? I'm migrating all my code and having trouble with the syntax. Thank You.

avatar image
14

Answer by Fritsl · Nov 01, 2017 at 02:50 PM

c# for the lazy :

     void ChangeMaterial(Material newMat)
     {
         Renderer[] children;
         children = GetComponentsInChildren<Renderer>();
         foreach (Renderer rend in children)
         {
             var mats = new Material[rend.materials.Length];
             for (var j = 0; j < rend.materials.Length; j++)
             {
                 mats[j] = newMat;
             }
             rend.materials = mats;
         }
     }

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 DraCieLisAWAL · May 30, 2020 at 09:34 PM 0
Share

Any idea how do I set em back to original ?

avatar image
0

Answer by metixgosu · Mar 16, 2021 at 07:53 PM

This code works but dont forget to add your parent game object name like.

      void ChangeMaterial(Material newMat)
      {
          Renderer[] children;
          GameObject parent;
          children = parent.GetComponentsInChildren<Renderer>();
          foreach (Renderer rend in children)
          {
              var mats = new Material[rend.materials.Length];
              for (var j = 0; j < rend.materials.Length; j++)
              {
                  mats[j] = newMat;
              }
              rend.materials = mats;
          }
      }


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 mitchellvandun · Jan 05 at 12:50 PM 0
Share

hi metixgosu,

could u maybe help me with my thread?

https://forum.unity.com/threads/unity-xr-radial-menu-button-material-manager.1220454/

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

27 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

Related Questions

Is it possible to change the element name in an Array List? 1 Answer

How do I move a gameObject to different layers? 1 Answer

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

Material Indices from Blender are different in Unity 2 Answers

How do I store the colors of children in an array? 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