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.
- 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)?
- Suppose I need to clean up. What code do I execute to clean up that material? Is
Destroy(Render.material);
correct? - 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’sGameObject.Destroy()
, or is there a callback I can use to execute code when the object is destroyed, likevoid 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.
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?
– Bifilar