I have a class BlinkingBehaviour
which inherits from MonoBehaviour
. Game objects attached to scripts containing classes that inherit from BlinkingBehaviour
should, as the name suggests, blink.
BlinkingBehaviour
is a small class so I will share the entirety of its code here:
public class BlinkingBehaviour : MonoBehaviour
{
private Renderer renderer;
public bool BlinkingEnabled;
public Color BlinkingColour = new Color(1.0f, 127f / 255f, 39f / 255f);
protected float CurrentColourScale;
protected float ColourScaleIncrementPerUpdate = 0.005f;
protected const float MaxColourScale = 2f;
protected void Start()
{
this.renderer = this.gameObject.GetComponent<Renderer>();
}
protected void Update()
{
if (this.BlinkingEnabled)
{
SetBlinkingColour();
}
else
{
SetColour();
}
}
protected void SetColour()
{
if (this.CurrentColourScale <= 1f)
{
this.renderer.material.SetColor("_EmissionColor", this.BlinkingColour * this.CurrentColourScale);
}
else
{
this.renderer.material.SetColor("_EmissionColor", this.BlinkingColour * (MaxColourScale - this.CurrentColourScale));
}
}
protected virtual void SetBlinkingColour()
{
SetColour();
this.CurrentColourScale += ColourScaleIncrementPerUpdate;
if (this.CurrentColourScale <= MaxColourScale)
{
return;
}
ResetBlinkingColourScale();
}
protected void ResetBlinkingColourScale()
{
this.CurrentColourScale = 0f;
}
}
I have a class Lamp
which inherits from BlinkingBehaviour
. I attached Lamp.cs
to an object in my scene. In my inspector, I checked the BlinkingEnabled
option, expecting to see the Lamp
game object blinking after I pressed "Play".
However, the object did not blink.
After setting a breakpoint in my Start
method, I could see that this.gameObject.GetComponent<Renderer>();
returned a valid component.
On the other hand, my breakpoints in my Update
method were not hit. (Yes, I have invoked base.Update()
in the Update
method in Lamp.cs
.)
What can I do to ensure that game objects in my scene exhibit blinking behaviour when attached to scripts that inherit from BlinkingBehaviour
?