How to change the radius of a light from a script?

Hello,

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.

I’m in the most recent version of Unity.

Code didn’t upload, let me try again:

So the 2d light is part of the URP package, and belongs to the namespace UnityEngine.Rendering.Universal: Class Light2D | Universal Render Pipeline | 17.5.0

So up where you have using UnityEngine; you will also need to add using UnityEngine.Rendering.Universal; on another line.

And the property to change would be Light2D.pointLightOuterRadius.

Please post code as text with code tags, not as images.

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;
    }
}

image

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;
    }
}

Worked, thank you! I had forgotten about referencing objects.