Change Material and its properties at runtime: Unity Tutorial

In this article, we will see how to change material and its properties at runtime in a unity application.

Let’s prepare a simple scene in unity for this article. I have just added a Sphere GameObject in the empty scene.

Change Material at Runtime

We can apply multiple materials on a GameObject in unity. You can see these materials in the Mesh Renderer component of the GameObject.

Change Material and its properties at runtime

To change the material at runtime, add below script to the Sphere GameObject.

Change Material and its properties at runtime

We can also change the materials array from the MeshRenderer component. Actually, the GetComponent<MeshRenderer>().material property returns the first element of the materials array only.

Change Material Properties at Runtime

Material properties is directly related to the shader attached to the material. You can see the shader attached to the material in the Inspector Window.

Change Material and its properties at runtime

If we create a new material then unity already attaches Standard shader to it. However, the shader attached to the material can be changed from the Inspector window and also at runtime.

The properties of the material can be changed by editing shader properties.  You can see the detailed list of Shader property in the Inspector.

Change Material and its properties at runtime

After clicking on EditShader in Inpector window, You can see the shader property name and its type (int, float, color etc.).

Change Material and its properties at runtime

Change Material and its properties at runtime

Change Texture of a Material

Change Main Texture

Change Material and its properties at runtime

Material.mainTexture is the main diffuse texture. This can also be changed by “_MainTex” property name for the texture from unity’s builtin standard shader. Many shaders uses more than one texture. In that case, make sure to use right texture property name to change it at the runtime.

You can use GetTexture property to get current main texture of the material.

Change Normal Map

Normal map can also be changed by SetTexture property of the material. “_BumpMap” is property name of the texture in the unity standard shader.

While setting the material texture using the Standard Shader, sometime you may need to use EnableKeyword to enable features of the shader before calling SetTexture property.

Change Metallic Texture

Change Color of the Material

Change Main material’s color

Use Material.color property to get and set the main material color.

The same can be also changed by SetColor property. “_Color” is the property name of the color in the Standard shader.

Change material color at real-time

Attach below code to the GameObject.

Result:

Enable Shader Property

Change Emission Values

As I mentioned earlier, some of the property should be enabled first; then only it can be changed. For example, to change the emission color, first we need to enable Emission property then it’s color can be changed.

Change Float Value

Many shader property can be changed using Material.SetFloat property. Example – Metaalic Range, UV Set.

Change Metallic Range

Change Shader at runtime

Use below code to change shader at the runtime. Make sure the shader name is correct otherwise it will throw null reference exception.

That’s it for this tutorial. I hope you get an idea about how to change material and its properties at runtime in unity application. Please post your comments for queries and feedback. Thanks for reading.

 

Share Button
The following two tabs change content below.
Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

Gyanendu Shekhar

Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

Leave a Reply

Your email address will not be published. Required fields are marked *