Search Unity

<Light2D> methods for changing the inner and outer radius

Discussion in '2D' started by AntonTsirov, Aug 17, 2019.

  1. AntonTsirov

    AntonTsirov

    Joined:
    Aug 14, 2019
    Posts:
    5
    Hello everyone,

    I am trying to manipulate Light2D variables in a script.
    I am able to change the intensity and color of the light through the script like this:
    upload_2019-8-17_14-45-44.png
    But I want to also be able to change these:
    upload_2019-8-17_14-46-46.png
    Is it possible to do that and what method should I call in the script?
    I am mostly interested in inner/outer radius.
     
  2. Pebbl3s

    Pebbl3s

    Joined:
    Apr 13, 2013
    Posts:
    6
    playerLight.pointLightInnerRadius and playerLight.pointLightOuterRadius seem to work for me. Couldn't find any documentation to back that up unfortunately
     
    Luis0413 and conuletv like this.
  3. yeoldecoot

    yeoldecoot

    Joined:
    Apr 18, 2020
    Posts:
    1
    Light2D doesn't seem to exist for me. did you make any changes to the original script

    Edit: in order to reference it now you need to modify a couple things in the original script.

    1. add these 2 lines to the top
    using System.Collections;
    using UnityEngine;
    2. add a public identifier to the Light2Dmanager class
    3. use the proper namespace in order to declare it properly
    UnityEngine.Experimental.Rendering.Universal.Light2D
     
  4. Carrot_Sensei

    Carrot_Sensei

    Joined:
    Jun 12, 2022
    Posts:
    1
    EDIT: the second string of code is the correct one.

    pointLightInnerRadius and pointLightOuterRadius work for me as well but for some reason i keep getting this error:

    NullReferenceException: Object reference not set to an instance of an object
    ChangeLightRange.Update () (at Assets/Scripts/Core/HarbingerOfColors/ChangeLightRange.cs:21)

    Even though it works inside the game.
    Here is my code.


    using UnityEngine;
    public class ChangeLightRange : MonoBehaviour
    {
    [SerializeField] private UnityEngine.Rendering.Universal.Light2D myLight;
    [SerializeField] private float rangeIncrease = 0.05f;
    [SerializeField] private float minRange = 0f;
    [SerializeField] private float maxRangeOuter = 25f;
    [SerializeField] private float maxRangeInner = 24.5f;
    private float startTime;

    private void Start()
    {
    myLight = GetComponent<UnityEngine.Rendering.Universal.Light2D>();
    }

    private void Update()
    {
    if (Input.GetKeyDown("e"))
    {
    myLight.pointLightInnerRadius = Mathf.Clamp(myLight.pointLightInnerRadius + rangeIncrease, minRange, maxRangeInner);
    myLight.pointLightOuterRadius = Mathf.Clamp(myLight.pointLightOuterRadius + rangeIncrease, minRange, maxRangeOuter);
    }
    if (Input.GetKeyDown("q"))
    {
    myLight.pointLightInnerRadius = Mathf.Clamp(myLight.pointLightInnerRadius - rangeIncrease, minRange, maxRangeInner);
    myLight.pointLightOuterRadius = Mathf.Clamp(myLight.pointLightOuterRadius - rangeIncrease, minRange, maxRangeOuter);
    }
    }

    }


    EDIT:

    I fixed it I don't know exactly why it happened but after an intense debug session I figured what was going. So the fix to the code above is this:


    using UnityEngine;
    public class ChangeLightRange : MonoBehaviour
    {
    [SerializeField] private UnityEngine.Rendering.Universal.Light2D myLight;
    [SerializeField] private float rangeIncrease = 0.05f;
    [SerializeField] private float minRange = 0f;
    [SerializeField] private float maxRangeOuter = 25f;
    [SerializeField] private float maxRangeInner = 24.5f;
    private float startTime;

    private void Start()
    {
    myLight = GetComponent<UnityEngine.Rendering.Universal.Light2D>();
    }

    private void Update()
    {

    if (Input.GetKey("e"))
    {
    if(myLight != null)
    {
    myLight.pointLightInnerRadius = Mathf.Clamp(myLight.pointLightInnerRadius + rangeIncrease, minRange, maxRangeInner);
    myLight.pointLightOuterRadius = Mathf.Clamp(myLight.pointLightOuterRadius + rangeIncrease, minRange, maxRangeOuter);
    }
    }
    if (Input.GetKey("q"))
    {
    if (myLight != null)
    {
    myLight.pointLightInnerRadius = Mathf.Clamp(myLight.pointLightInnerRadius - rangeIncrease, minRange, maxRangeInner);
    myLight.pointLightOuterRadius = Mathf.Clamp(myLight.pointLightOuterRadius - rangeIncrease, minRange, maxRangeOuter);
    }
    }
    }
    }
     
    Last edited: Jul 14, 2022