Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Turn off LWRP 2D Light with Script

Discussion in '2019.2 Beta' started by Patr1ck1, Jun 25, 2019.

  1. Patr1ck1

    Patr1ck1

    Joined:
    Mar 11, 2019
    Posts:
    3
    Hello,
    how can I disable a 2D Light (Script) from the LWRP?

    upload_2019-6-25_16-50-53.png

    E.g. turning off the "Light 2D (Script)" with the "Flashlight Script".

    Thank you in advance
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,620
    According to the Light2D source code, it inherits from MonoBehaviour, thus you should be able to turn off the Component with
    light2d.enabled = false;
    Where 'light2d' is a variable name that points to the actual Light2D in this example.
     
    LeonhardP and simons_unity like this.
  3. Patr1ck1

    Patr1ck1

    Joined:
    Mar 11, 2019
    Posts:
    3
    That's right, but my script can't refer to the Light Script because it's in another directory than the Flashlight Script. How can I move one to the other?
     
  4. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,620
    I don't think I understand what the problem is, but here is a code snippet that hopefully helps you further. If it doesn't help, please describe the problem in more detail.

    If you add a "Light2D" field to the Flashlight Component and then assign the Light2D in the Inspector, you can manipulate it inside the Flashlight code as shown below.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. class Flashlight : MonoBehaviour
    4. {
    5.     [SerializeField]
    6.     UnityEngine.Experimental.Rendering.LWRP.Light2D m_Light2D = null;
    7.    
    8.     void Update()
    9.     {
    10.         m_Light2D.enable = false;          
    11.     }
    12. }
     
    insoluzioni, LeonhardP and Patr1ck1 like this.
  5. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,983
    If I have understood you correctly, you are unsure how to access data from one script in another? If that is the case, I recommend following the tutorials in the learn section.

    You can get a reference by using a public reference and slotting it in the inspector, such as what @Peter77 has shown above.

    Alternatively you can make the script a static singleton (not recommended, and in general will create spaghetti code) and access it anywhere. Both of these are touched on in the basic tutorials. Good luck!
     
  6. Patr1ck1

    Patr1ck1

    Joined:
    Mar 11, 2019
    Posts:
    3
    That worked, thank you :D
     
    JCocker and Peter77 like this.