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.
Here is my Lamp
class:
public class Lamp : BlinkingBehaviour
{
private void Start()
{
base.Start();
}
private void Update()
{
base.Update();
}
}
After setting various breakpoints, I could see that the Start()
method was hit in both Lamp
and BlinkingBehaviour
. I could also see that this.gameObject.GetComponent<Renderer>();
returned a valid component.
On the other hand, my breakpoint in the Update
method in Lamp
was never hit.
What can I do to fix this issue?
UPDATE:
I deleted the Start()
and Update()
methods from my derived class. I discovered something new: The Update()
method in BlinkingBehaviour
is never called, even though Start()
is.
Update
is private, and it can beprotected
orpublic
, in this case it doesn't really matter since the superclass' callback is called anyway if there's no overridingUpdate
method in the inheriting class. \$\endgroup\$