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 diglungdig · Jul 12, 2017 at 05:47 PM · scripting problemmaterialmaterialsrendererreferencing

Change the materials of a list of game objects to one material, and then change it back to their old ones.

I want to change all of materials on a game object's child objects to a simple silhouette material, and then change them back to the old ones when user inputs a certain command. My functions are as below:

 public List<Material> mats;
 public Material SilhouetteMaterial;
 
      public void AttachSilhouette()
         {
             mats = new List<Material>();
 
             var childrenRenderers = objects.GetComponentsInChildren<Renderer>();
 
             foreach (var childRenderer in childrenRenderers)
             { 
        
                //Material NewOne = new Material(childRenderer.material);
                //mats.Add(NewOne);
                 
                 mats.Add(childRenderer.material);
                 childRenderer.material = SilhouetteMaterial;
             }
         }
         public void DetachSilhouette()
         {
             var childrenRenderers = objects.GetComponentsInChildren<Renderer>();
             int t = 0;
             foreach (var childRenderer in childrenRenderers)
             {
                 childRenderer.material = mats[t];
                 t++;
             }
             mats.Clear();
 
         }

However this problem is, executing function AttachSilouette() seems to give me a list of silhouette material instead of the original materials on these game objects that I want to cache. If I comment out the line childRenderer.material = SilhouetteMaterial; then it will actually give me a list of the materials that I want to cache. Can someone tell me why this is happening? I figured that maybe the list only stores a list of reference so I tried creating a material instance before adding it to the list however it doesn't work either.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by CheetahSpeedLion · Jul 13, 2017 at 01:53 AM

I'm not sure but you might be saving a reference to the renderer's material slot instead of the actual material in that slot. Try making a new material in your list by using the Material constructor and copying the "original Material" instead of referencing it https://docs.unity3d.com/ScriptReference/Material-ctor.html

Alternatively, play around with switching shaders instead of materials. I do this in my game for a similar effect. See "flashing effect" in Google for examples.

Areleli

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

120 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

Related Questions

Changing two different objects renderer colour 1 Answer

How to compare 2 materials? 2 Answers

Issue with Mesh Renderer and Skinned Mesh Renderer 0 Answers

Having a problem with the second material (on the same renderer) 0 Answers

How do I change the material of a prefab when it is not in the hierarchy? 0 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