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 TimBorquez · Sep 15, 2013 at 09:24 PM · arraymaterialmaterialsarraysindex

Changing the material of all children, then change back to old one

im trying to change the material of every child to certain shader when using a cloaking device, then when the device is off reassign the old one

this function is on the cloak device on the player and switches all of the materials to the cloak one:

 var cloakMat : Material;
 
 function CloakMaterial () {
 
     var allChildren = transform.parent.gameObject.GetComponentsInChildren(Transform);

     for (var child : Transform in allChildren)
     {
 
         if(child.renderer != null)
         {
             child.renderer.material = cloakMat;
         }
     }
 }

my problem is trying to make an array filled with all of the old materials and putting them back

or should i try to just put the cloak material in the built in "materials" array in the renderer component and then switch back and forth from there?(this would probably be a lot more work for me and i would prefer not to)

Comment
Add comment · Show 1
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 TimBorquez · Sep 15, 2013 at 09:32 PM 0
Share

another solution would be to add the cloak material at the end of the index for the built in materials array

then i could maybe switch back to the previous array number to switch the material back

i still kinda need help carrying that out though...

1 Reply

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

Answer by supernat · Sep 16, 2013 at 01:53 AM

In the Start() method, you should create a new material array, then assign the existing material before cloaking it, something like follows (sorry my JS is very rusty, so this is in C# but should be easy to transpose):

 Material[] prevMats;
 
 void Start()
 {
   prevMats = new Material[transform.parent.childCount];
 }


Since we need to keep the materials ordered, we can just create an index variable and set it to 0 outside of the for() loop in your CloakMaterial() method. Now, the caveat is that I am assuming your transforms will always be returned in the same order. This should generally be acceptable as long as you aren't creating/destroying transforms dynamically. If you see the wrong textures applied after coming back from a cloak, it means the index no longer matches what it did when you saved the material off. Then we can use a Dictionary or some other method to associate each child transform with its material.

Then, just above the child.renderer.material = cloakMat, add the following line:

 prevMats[indx++] = child.renderer.material;

Now, create a ReturnToNormal() method that rotates through the children and sets their materials back to prevMats[indx++] in the same way.

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 TimBorquez · Sep 16, 2013 at 05:28 AM 0
Share

thanks for such a detailed response, pretty sure i can handle it with this info, thanks a lot! :)

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

16 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

Related Questions

Array of transform index out of range error 1 Answer

Array Within Index 1 Answer

How to change specific material shared among specific children. 1 Answer

With a second material on an object, how do I restrict it to only 1 part of the object? 0 Answers

How to find the index of an Object in an Array 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