Search Unity

I want to change 2D Light properties through codes

Discussion in '2D' started by mehrdadk36, Nov 26, 2020.

  1. mehrdadk36

    mehrdadk36

    Joined:
    Nov 26, 2020
    Posts:
    3
    Hi everyone!
    So here I have a very frustrating problem with 2D lighting. It's an Experimental feature in Unity 2019 and as you may know in the inspector window it's being shown as a script not a built-in feature.
    So, in 3D Lighting, you can simply make a Light reference in your code and then access to any properties of your lighting through that reference. But in 2D lighting I just can't do that. I can't make a reference in my code to change the properties of a, say, 2D Point Light.
    I'm a beginner. If anyone can help me with this, please go ahead and give me your advice.
     
  2. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,511
    You should be able to do that.

    Here's a simple test case:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Rendering.Universal;
    3.  
    4. public class TestLight : MonoBehaviour
    5. {
    6.     public Light2D light;
    7.  
    8.     void Update()
    9.     {
    10.         light.intensity = Mathf.PingPong(Time.time, 1f);
    11.     }
    12. }
    13.  
    edit: UnityEngine.Experimental.Rendering.Universal (old Unity versions) has become UnityEngine.Rendering.Universal instead (modern Unity versions). I updated the code sample above.
     
    Last edited: Jun 13, 2023
    XhryZed likes this.
  3. mehrdadk36

    mehrdadk36

    Joined:
    Nov 26, 2020
    Posts:
    3
    Thank you SO MUCH Lo-renzo.
    I didn't know I should add "using UnityEngine.Experimental.Rendering.Universal" at the beginning.
     
  4. angeldevelopment

    angeldevelopment

    Joined:
    Sep 28, 2022
    Posts:
    247
    It is now UnityEngine.Rendering.Universal ... Experimental has been removed
     
  5. Silvak9119

    Silvak9119

    Joined:
    Feb 18, 2022
    Posts:
    1
    You are a saint for providing this update.
     
    angeldevelopment likes this.
  6. angeldevelopment

    angeldevelopment

    Joined:
    Sep 28, 2022
    Posts:
    247
    I had the same issue myself lmao!
     
  7. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,511
    OK I updated my old answer so there's no confusion any more :p