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 BigScary · Nov 28, 2016 at 09:50 PM · materialdestroyrenderer

How to clean up after changing Renderer.material?

This API documentation says:

If the material is used by any other renderers, this will clone the shared material and start using it from now on. This function automatically instantiates the materials and makes them unique to this renderer. It is your responsibility to destroy the materials when the game object is being destroyed.

  1. How to I determine whether or not Unity actually created a new material that I then need to clean up (the docs say an instance will be created if necessary)?

  2. Suppose I need to clean up. What code do I execute to clean up that material? Is Destroy(Render.material); correct?

  3. How do I guarantee that my cleanup code will run - do I need to write a custom public void DestroyMe(){ ... } method which cleans up and then calls Unity's GameObject.Destroy(), or is there a callback I can use to execute code when the object is destroyed, like void OnDestroy(){ ... }?

By the way, notice the example in the above linked documentation is actually an example about Renderer.sharedmaterial, and so it doesn't actually demonstrate how to use Renderer.material correctly. :(

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

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

Answer by BigScary · Nov 30, 2016 at 04:52 PM

Alright so given the uncertainty, here's some defensive coding that works.

  1. Anytime you reference the Renderer.material property, make a note (private boolean field?) that you did so.

  2. In your script's OnDestroy() method, if you accessed the Renderer.material property, call Destroy(myRenderer.material) to clean up.

This guarantees any clones created will be cleaned up when your object is destroyed. If there were no clones created because there were no other objects in your scene using the shared material, then before your object is destroyed, it will first generate a clone and then immediately destroy that clone. That's wasteful, but worth the guarantee of avoiding material leaks in my opinion. If you're concerned about that waste, you may be able to mitigate it via object pooling.

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
1

Answer by tanoshimi · Nov 28, 2016 at 10:30 PM

  1. The docs don't say "if necessary" (not that I could see). They say "This function automatically instantiates the materials".

  2. Yes, that's right.

  3. Right again.

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 BigScary · Nov 30, 2016 at 04:44 PM 0
Share
  1. Yes, but above that it says "If the material is used by any other renderers, this will clone the shared material and start using it from now on."

So it's hard to know whether a clone was made or not, unless there's some provided API for figuring that out which I don't know about?

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

63 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

Related Questions

Changing two different objects renderer colour 1 Answer

Why does my materials get destroyed on some persistant objects ? 2 Answers

Destroy mesh / material after attaching to renderer 0 Answers

SetPropertyBlock doesn't work for particle systems? 0 Answers

Problem with sprite animation on android 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