I’m a beginner to Unity. Like, watched-my-first-tutorial-last-week beginner.
I’ve actually been doing decently well so far but I’m a bit stuck and I couldn’t find the answer anywhere in the Unity Docs:
How could I change the outer radius of the 2D light on an object within a MonoBehaviour Script? I need some light to illuminate a surface only when the emitting object is inside the surface so I want to change the light’s radius as the object enters to create a nice transition.
This is my code with the obviously incorrect attempt to change the outer radius of the light. I tried using the syntax for the transform changes as a reference but couldn’t get it exactly.
Thank you for the help, and sorry for the late-ish reply. I unfortunately ran into another error while testing:
using UnityEngine;
using UnityEngine.Rendering.Universal;
public class ManageSpecialBulletLight : MonoBehaviour
{
public float maxRadius;
// Update is called once per frame
void Update()
{
GameObject.Light2D.pointLightOuterRadius = maxRadius;
}
}
This is the inspector window for my object I’m applying this code to:
I assume “does not contain a definition for Light2D” means it couldn’t find the Light2D component of the object? Which doesn’t make sense to me, since I added the universal rendering reference at the top and attached the script to an object with the Light 2D.
I checked through the Hierarchy window and as far as I can tell, only that object contains my script, so it shouldn’t be trying to run on an object without Light 2D.
It’s not a static property. You need a reference to an instance of a Light2D object. So reference the light via the inspector and access it correctly:
using UnityEngine;
using UnityEngine.Rendering.Universal;
public class ManageSpecialBulletLight : MonoBehaviour
{
public float maxRadius;
public Light2D light;
private void Update()
{
light.pointLightOuterRadius = maxRadius;
}
}