How to change material of an object into material that already exist?

Hi! I know that there is a lot of topics about this but I couldn’t find a topic about how to change material of an object into material that already exist. How to reach it in C#? Thanks!

You can use Resources.Load. Make sure to create a Resources folder in your asset folder.

As an example:

// Assigns a material named “Assets/Resources/changeMaterial” to the object.

Material newMaterial = Resources.Load(“changeMaterial”, typeof(Material)) as Material;

gameObject.renderer.material = newMaterial;

Typically if the material already exists, you would use drag and drop to initialize a public variable. Attach this script to an game object. Select the object and then drag and drop the material you want to change to on the ‘mat’ variable.

public Material mat;

void Start() {
    renderer.material = mat;    
}