Materials - Clone?

Hello,
for my better understand i would like here ask again about materials and leak, how its correct working then i don´t would like create a memory leak…
Have 2 asks / options.

A)
I use instantiate for create a gameobject from a prefab (Resources.Load).
After create, i change the color = the material is then a copy.
Must i only use destroy for the material copy or must i destroy the hold material too?

B)
I use instantiate for create a gameobject from a prefab (Resources.Load) but this don´t have a material.
After create, i create a material with resources.load and attached it on the other gameobject.
When i now change the color from this materal what i have loaded, its then again a copy or it is the same?

With best regards,
Creepi

Anytime you change a material through a renderer, like:

someObject.renderer.material.SetColor("_SomeColor", Color.red);

You’ll create an instance of the material.

If you change a material through a renderer using .sharedMaterial you will modify the base material directly, and not create an instance.

If you modify a material property directly you won’t create an instance.

9 Likes

But when i change with sharedMaterial, all other gameobject with this material will be change the color?

First, when i get a copy/instance from a material after change the color, when i then again change the color from the same gameobject, then its use automatic the copy or its then a copy from a copy?

And other: before i destroy the gameobject, i destroy then the instance materials and then the gameobject.
When i no have other gameobjects with this material (original) if this then destroy too when destroy the gameobject?
Or stay this material into the memory :eyes:

Example:
Create GameObject (prefab…)
change the color and so i get a instance from it, its a copy.
Only this color on this gameobject would be change…
Now i would like delete the gameobject, so i delete frist his material and then this gameobject.
But what is with the other material, not the copy…?
Or is this delete automatic when change a scene?

What when i then again create this gameobject, use it then the frist material or have i now it double?

…„If you modify a material property directly you won’t create an instance.“ this is when i use option B from my ask?

1 Like

If you have something like:

public Material myMaterial;

void Start()
{
     myMaterial.SetColor("_SomeColor", Color.red);
}

That won’t make an instance of myMaterial.

If you do:

public Renderer myRenderer;

void Start()
{
     Material newMaterial = new Material();
     myRenderer.material = newMaterial;
    
     //This is fine:
     newMaterial.SetColor("");

     //This will cause an instance:
     myRenderer.material.SetColor("");
}

It is a better practice to clean stuff up yourself if you are making instances at runtime, I wouldn’t rely on Unity to clean those up. If you are loading a new scene all your instances will get lost.

You should read the manual, especially regarding MaterialPropertyBlocks.

8 Likes

Thank you very much for the examples and the note with the MaterialPropertyBlock, thats have help me. :slight_smile:

(Necroing this because it comes up on web searches.)

The problem with MaterialPropertyBlock is it doesn’t work with the SRP Batcher.

I am thinking that passing the material you want to clone to the Material constructor could work. That’s what I have tried and it seems to be doing the trick so far.

Material terrainTypeMaterial = new Material(defaultMaterial);
9 Likes