Android Unity Game Development Blog: Making Flashing Materials

Covyne Entertainment
1 min readJan 31, 2021

Problem:

I’d like to make the material of a GameObject in Unity appear as though it’s flashing. Should be able to add this to any GameObject with a MeshRenderer.

Solution:

We’r going to make a Class that we can attach to any GameObject with a MeshRenderer. This Class will take the existing colour of the material as its starting color, then increase and decrease the alpha to make a flashing effect.

Here is the code. It’s all pretty self explanitory, if you’d like more info, pop a comment and I’l attempt to answer your question(s).:

using UnityEngine;public class FlashingMeshRenderer : MonoBehaviour
{
public float FlashSpeed;
public float MaxAlpha;
public float MinAlpha;
private Material material;
private bool increase;
private Color startColor;
void Start()
{
material = GetComponent<MeshRenderer>().material;
startColor = material.color;
}
void Update()
{
Color color = material.color;

float fade = increase ?
color.a + (FlashSpeed * Time.deltaTime) :
color.a - (FlashSpeed * Time.deltaTime);
material.color =
new Color(startColor.r, startColor.g, startColor.b, fade);

if (fade >= MaxAlpha)
{
increase = false;
}
if (fade < MinAlpha)
{
increase = true;
}
}
}

--

--