Skip to main content
deleted 265 characters in body
Source Link
Taco
  • 326
  • 1
  • 17

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.colors from occurring.

You're setting the color of a Renderer material, not that of a light.

public class gradient : MonoBehaviour
{
    public Gradient myGradient;
    public float strobeDuration = 2f;

    public void Update()
    {
        float t = Mathf.PingPong(Time.time / strobeDuration, 1f);
        GetComponent<Renderer>().material.color = myGradient.Evaluate(t);

        // Attempt to get a light and set its color.
        if (TryGetComponent(out Light light))
            light.color = myGradient.Evaluate(t);
    }
}

I also recommend using TryGetComponent for your Renderer too as the object may not have a renderer, in which case you'll get a NullReferenceException with the way your code is written when trying to access .material.color.

Your light's color isn't changing because it uses the Light component, not Renderer. Simply add support for the Light component:

if (TryGetComponent(out Light light))
    light.color = myGradient.Evaluate(t);

With that said, I'd recommend that you use TryGetComponent for working with the Renderer component too:

if (TryGetComponent(out Renderer renderer))
    renderer.material.color = myGradient.Evaluate(t);

The use of TryGetComponent will help prevent potential NullReferenceExceptions from occurring.

Source Link
Taco
  • 326
  • 1
  • 17

You're setting the color of a Renderer material, not that of a light.

public class gradient : MonoBehaviour
{
    public Gradient myGradient;
    public float strobeDuration = 2f;

    public void Update()
    {
        float t = Mathf.PingPong(Time.time / strobeDuration, 1f);
        GetComponent<Renderer>().material.color = myGradient.Evaluate(t);

        // Attempt to get a light and set its color.
        if (TryGetComponent(out Light light))
            light.color = myGradient.Evaluate(t);
    }
}

I also recommend using TryGetComponent for your Renderer too as the object may not have a renderer, in which case you'll get a NullReferenceException with the way your code is written when trying to access .material.color.