You're setting theYour light's color of aisn't changing because it uses the RendererLight
materialcomponent, not that of a lightRenderer
. Simply add support for the Light
component:
public class gradient : MonoBehaviour
{
public Gradient myGradient;
public float strobeDuration = 2f;
public voidif Update()
{
float t = Mathf.PingPongTryGetComponent(Time.time /out strobeDuration,Light 1flight);)
GetComponent<Renderer>().materiallight.color = myGradient.Evaluate(t);
// Attempt to get a light and set its color.
With that said, I'd recommend that you use TryGetComponent
for working with the Renderer
component too:
if (TryGetComponent(out LightRenderer lightrenderer))
lightrenderer.material.color = myGradient.Evaluate(t);
}
}
I also recommend usingThe use of TryGetComponent
for your Renderer
too as the object may not have a renderer, in which case you'll get awill help prevent potential NullReferenceException
with the way your code is written when trying to access .material.color
s from occurring.