How to change Material and its properties at runtime in Unity?

Ouzani Abd Raouf
3 min readJun 26, 2020

--

In unity, we can apply multiple materials on a GameObject (if the gameObject mesh has multiple submeshes). These materials are listed in the “Mesh Renderer” component of the gameObject.

To change the material at runtime, add the following script to a gameObject with a Mesh Render component.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeMaterial01 : MonoBehaviour
{
public Material newMaterial; //You must drag your new material here in the inspector window
void Start () {

// Get the Mesh Renderer Component from this gameObject
MeshRenderer meshRenderer = GetComponent<MeshRenderer>();
// Get the current material applied on this GameObject
Material oldMaterial = meshRenderer.material;
//print the material name in the console
Debug.Log("Applied Material: " + oldMaterial.name);
// Set the new material on the GameObject
meshRenderer.material = newMaterial;
}
}

The GetComponent<MeshRenderer>().material property returns the first element of the materials array only. If you have more than one material applied to your gameObject, you can change the materials array from the MeshRenderer component with this code:

public Material[] newMaterials;
{...
meshRenderer.materials = newMaterials;
...}

How to change Material Properties at runtime in Unity?

The Material properties are directly related to the shader attached to the material, so they can be changed by editing the shader properties. You can see the detailed list of the Shader properties and their types in the Inspector by clicking on “Edit Shader…”.

How to change a Material texture?

For example, to change the main texture of a Material, you have two options:

a- By accessing the mainTexture property:

public Texture2D myTexture;private void Start()
{
GetComponent<MeshRenderer>().material.mainTexture = myTexture;
}

b- By using the “SetTexture()” function:

public Texture2D myTexture;private void Start()
{
MeshRenderer rend = GetComponent<MeshRenderer>();
rend.material.SetTexture("_MainTex", myTexture);
}

You can use the “GetTexture()” function to get current main texture of the material.

rend.material.GetTexture("_MainTex");

You may need to use the “EnableKeyword” function to enable a property keyword of the shader before calling “SetTexture()” function.

m_Renderer.material.EnableKeyword ("_METALLICGLOSSMAP");meshRenderer.material.SetTexture("_MetallicGlossMap", myTexture);

How to change a Material color?

In the same way with textures, to change the main color of a Material, you have also two options:

a- By accessing the color property:

Color newColor = Color.blue;
GetComponent<MeshRenderer>().material. color = newColor;

b- By using the “SetColor()” function:

MeshRenderer rend = GetComponent<MeshRenderer>();
rend.material. SetColor("_Color", Color.blue);

Use the “GetColor()” function to get the main color of the material.

rend. material.SetFloat("_BumpScale", 0.5f);

How to change the Shader at runtime?

Finally you can change the shader at runtime using the following script:

Shader BumpedDiffuse = Shader.Find("Mobile/Bumped Diffuse"); 
GetComponent<MeshRenderer>().material.shader = BumpedDiffuse;

Thank you for reading my article. If you like it, please share it.

See also :

--

--

Ouzani Abd Raouf
Ouzani Abd Raouf

Written by Ouzani Abd Raouf

Computer Science Engineer and Cisco Instructor

No responses yet