Introduction
Although material colors can be changed from the Inspector, this article will explain how to change them dynamically from a script during gameplay.
What are materials?
Materials determine the texture and appearance of objects, such as metal, plastic, wood, and patterns. By changing these parameters, you can freely change the appearance of objects.
The Unity Asset Store offers a wide variety of materials.

Let’s create a material
Materials can be created by right-clicking in the Project View > Create > Material.

Set the material to the object
Drag and drop the created material onto the object. Simply drag and drop it into the hierarchy, inspector, or scene view.

Let’s change the color of the material from the inspector.
You can change the color from Albedo in the material inspector.

As you can see, colors can be freely changed in the editor, but here we will show you how to change them from the script.
How to switch materials from a script
Here’s how to switch materials from a script. The advantage is that you can easily change not only the color but also the texture and feel itself.

The following is an example of a script for switching materials. When the space bar is pressed, the material will switch to the one that has been set in advance in the inspector.

using UnityEngine;
public class ColorChange : MonoBehaviour
{
[SerializeField] Material mat = default;
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
this.GetComponent<MeshRenderer>().material = mat;
}
}
}

The advantage is that you can change not only the color but also all the properties of the material. However, if you want to change to various colors, the number of materials will increase accordingly, which will increase the size of the game.
Switching between multiple materials
Earlier, we switched between materials only once, but now we will try switching between multiple materials using an array.
Each time you press the space bar, the material will change to a different one in order.
using UnityEngine;
public class ColorChange : MonoBehaviour
{
[SerializeField] Material[] mat = new Material[3];
private int count = 0;
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
this.GetComponent<MeshRenderer>().material = mat[count];
count++;
if(count == mat.Length)
{
count = 0;
}
}
}
}
Prepare three types of materials and set them in the inspector.

When executed, the materials will change in order.

To dynamically change only the color of a material from a script
If you only want to change the color, you can do so without switching materials.
Change the color property directly (SetColor)
This method allows you to change the color property of a material directly from a script. This way, you can change the color without increasing the number of materials.
To change it from a script, obtain the MeshRenderer component and use the material.SetColor function. Press the spacebar to change the color from the material properties.
using UnityEngine;
public class ColorChange : MonoBehaviour
{
[SerializeField] Material mat = default;
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
this.GetComponent<MeshRenderer>().material.SetColor("_Color", Color.blue);
}
}
}
How to specify colors
Colors can be easily specified using Color.blue, etc., but for more details, please see here.
If you want to set RGBA values, define the colors in advance and use them.
Color customColor = new Color(0.4f, 0.9f, 0.7f, 1.0f);
About the first argument of SetColor
In the above example, the first argument of SetColor is set to “_Color,” but this varies depending on the shader set for the material.
To check this, click the settings in the upper right corner of the material inspector and open Select Shader.

Then, the shader will open and the property name for setting the color (_Color) will appear.


If “_Color” doesn’t work, check here!
Points to note about .material
We have been using .material up to this point, but there is one point to keep in mind.
.material automatically creates new materials by copying the original materials. Automatically created materials remain even after the game object is deleted, so leaving them as they are will cause memory leaks.
Memory leak countermeasure 1
As a countermeasure, delete the material in the OnDestroy method when deleting the object.
using UnityEngine;
public class ColorChange : MonoBehaviour
{
[SerializeField] Material mat = default;
Material material;
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
material = this.GetComponent<MeshRenderer>().material;
material.SetColor("_Color", Color.blue);
}
}
private void OnDestroy()
{
if (material != null)
{
Destroy(material);
material = null;
}
}
}
Memory Leak Countermeasure 2
Some people may find it tedious to delete materials every time in OnDestroy().
In that case, you can use .sharedMaterial as a countermeasure. Simply replace “.material” with “sharedMaterial.” This allows you to modify the parameters of the original material without creating a copy of the material.
using UnityEngine;
public class ColorChange : MonoBehaviour
{
[SerializeField] Material mat = default;
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
this.GetComponent<MeshRenderer>().sharedMaterial = mat;
}
}
}
To change the texture of a material
You can also change the texture of a material from a script. For details, please see here.
Conclusion
That concludes our introduction to changing material colors! You can easily change colors with a single line of script, but when using “.material,” keep in mind that materials are automatically copied and created, so use it with caution when using it frequently.