3

In my Unity 2D game, I have a character with a lightsource component 'Light 2D(Script)' from the Lightweight RP package.

I want to change the intensity of the Light 2D with the code below. But I can't assign the 'Light 2D(Script)' to the public Light LightSource in the Unity Inspector panel.

I've tried using public Light2D LightSource class but it doesn't seem to exist.

Is there any other way to access the 2D Light component or something I'm doing wrong? I've also added a screenshot of the Inspector pane, if it helps.

If there's any more information you need just tell me and I hope someone can help. Thanks

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class Lighting : MonoBehaviour
{
    public Light LightSource;
    public float lightIntensity;
    public float minIntensity = 0.35f, maxIntensity = 0.65f;



    void Update()
    {
        lightIntensity = Random.Range(minIntensity, maxIntensity);
        LightSource.intensity = lightIntensity;
    }
}


Character Inspector Screenshot

neal
  • 31
  • 1
  • 1
  • 3
  • Do you have any errors, have you connected the Light in the editor? – Milos Romanic Sep 26 '19 at 16:37
  • 1
    your question isn't very clear what you actually have in the scene. A screenshot would help a lot. Anyway try changing the line from `public Light LightSource;` to `public Light2D LightSource;` then dragging the `Light2D` component onto it in the inspector. – Ruzihm Sep 26 '19 at 16:37
  • Yes, probably by using the correct field type ... though I don't know any `Light2D` class and it doesn't appear in the API. Where do you have it from? – derHugo Sep 26 '19 at 18:31
  • @derHugo I'm not sure either but I kind of assumed it was perhaps [the LWRP Light2D Component](https://docs.unity3d.com/Packages/com.unity.render-pipelines.lightweight@6.7/api/UnityEngine.Experimental.Rendering.LWRP.Light2D.html) – Ruzihm Sep 26 '19 at 21:03
  • @Ruzihm or [this](https://assetstore.unity.com/packages/tools/particles-effects/light2d-gpu-lighting-system-30953) ;) but yeah yours seams closer – derHugo Sep 26 '19 at 21:04
  • My code editor doesn't show any errors but I can't assing the Light2D to the `public Light LightSource;` – neal Sep 27 '19 at 13:06
  • @Ruzihm VisualStudio doesn't give me the option of `public Light2D LightSource;`. And I've added a Screenshot of the Inspector, if it helps. – neal Sep 27 '19 at 13:07
  • @Musaka the problem is that I can't drag the Light2D Component to the `LightSource` slot – neal Sep 28 '19 at 11:00
  • your variable name ("lightSource") *must* start with a lower case letter – Fattie Sep 30 '19 at 11:45

7 Answers7

5

Assuming you are using the universal render pipeline for 2D lighting this is the solution I found.

Within the Light2D script (which you can open by clicking the three vertical dots in the top right of the Light2D component and hitting edit script) there is the declaration of the namespace UnityEngine.Experimental.Rendering.Universal In order to reference Light2D simply at the top of your script write

using UnityEngine.Experimental.Rendering.Universal;

There may appear to be an error when doing this, but press on. From here on out you'll be able to reference the Light2D component much as you would any other component, such as using GetComponent<Light2D>() If any of this gives an error within your code editor still compile to test if it works, I have a fully functioning script using this method but Visual Studio still believes that Light2D does not exist. Regardless, this appears to work and I hope this works for everyone else as well.

Mr. Doctorb
  • 51
  • 1
  • 1
3

Ok bud i got your answer.

First off, u need to open the Light2D(script) do the following:

1.) add the following to the top of the script:

using System.Collections;
using UnityEngine;

2.) you need to set the class of the Light2D script to "public" (so u can access it in your "lighting.cs" script.):

public class Light2DManager : IDisposable

3.) once that is done save the light2d script.


Ok now we are going to access it...

in your Lighting script we do the usual when we want to access another script:

GameObject TheLight = GameObject.Find("The Gameobject the script is on");

UnityEngine.Experimental.Rendering.LWRP.Light2D The2DLights = TheLight.GetComponent  <UnityEngine.Experimental.Rendering.LWRP.Light2D> ();

Now you should have access, just type the following to confirm:

The2DLights. (and you will get options like intensity etc etc)

This is my first time ever answering a question. turns out i was having the same issue, happy to help.

ez_moneyy
  • 31
  • 2
  • Hey @ez_moneyy , – neal Sep 30 '19 at 14:48
  • Sorry for that first comment, I keep forgetting that pressing enter saves the comment – neal Sep 30 '19 at 14:57
  • Hey @ez_moneyy , thanks for your answer. I went through the steps you told me to do but I got few problems in the part of accessing the script. If the Light2D component i'll be refencing to the LightSource in the Unity Inspector, is (of course) called LightSource which parts of your example code will I have to replace and is there anything else i'll have to change? Sorry for bothering you with such stupid questions but thanks again for your help:-) – neal Sep 30 '19 at 14:58
  • @neal sorry bud, but unity does not recognize light2d as a light source that you can prefab and drag into a public variable. youre gonna have to access the light2d script that u have on a game object with your script and add a random range to it, like this: Lights.intensity = Random.Range(2f, 5f); after the first 3 steps i give, you should be able to access and manipulate the variables in the light2d script with the final section of my answer. add me on discord and ill walk you through it Lunatiq #0247 – ez_moneyy Sep 30 '19 at 17:37
  • So I should add the code for the changing the intensity to the Light2D component script, instead of making an extra script for it? – neal Oct 01 '19 at 11:45
2

Ran into this today, to expand on this solution of the using statement (simply adding it did not work in my case, on 2020.2) at the top you can do:

using Light2DE = UnityEngine.Experimental.Rendering.Universal.Light2D;

Then use Light2DE as the class instead of Light2D in your script.

light = transform.GetComponent<Light2DE>();
ChaseWille
  • 21
  • 1
1

I don't understand the question very clearly, but it's possible you're just looking for "GetComponent".

Here's a random example,

public class CamToUI:MonoBehaviour
    {
    [System.NonSerialized] public WebCamTexture wct;
    public Text nameDisplay;

    private RawImage rawImage;
    private RectTransform rawImageRT;
    private AspectRatioFitter rawImageARF;
    private Material rawImageMaterial;

    void Awake()
        {
        rawImage = GetComponent<RawImage>();
        rawImageRT = rawImage.GetComponent<RectTransform>();
        rawImageARF = rawImage.GetComponent<AspectRatioFitter>();
        }

GetComponent finds "thing" (such as a "light2D") which is attached to that same game object.

• You have some game object

• A script, Script.cs, is attached to that game object

• Some other thing (say, Light2D) is attached to that same game object

Inside the script Script.cs, you can find the other thing, using GetComponent.

Ideally, don't call GetComponent repeatedly, just call it once in Awake or whatever. (But you're a mile away from worrying about performance, so don't worry about it.)

Fattie
  • 27,874
  • 70
  • 431
  • 719
1
using UnityEngine.Experimental.Rendering.Universal;
using UnityEngine;

public class Light2DController : MonoBehaviour {

    void Awake() {
        //assuming You have Light2D component in the same object that has this script
        Light2D light = transform.GetComponent<Light2D>();
    }
}

Unity 2019.3.0f1

Anton Kasabutski
  • 355
  • 5
  • 16
0

Here's how you can access the Light2D(Script) components.

Unity Version: Unity 2020.1.4f1

Import using UnityEngine.Experimental.Rendering.Universal; into your Script.

Please find the below code example.

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.Rendering.Universal;//Important
public class Lighting : MonoBehaviour
{
    
    Light2D theLights; // To Access the Light Components.

    public float maxRange = 0.54f;
    public float minRange = 0.31f;
    public float flickerSpeed = 0.5f;

    void Update()
    {
       //Assuming You have Light2D component in the same object that has this script.
       theLights.intensity = Mathf.Lerp(minRange, maxRange, Mathf.PingPong(Time.time, flickerSpeed));
      // The above Line is to create a flickering effect, You can also set a value like for example: theLights.intensity=0.5f, To set the intensity of the Light.
    }
}

You can also access other Light2D components similar way

theLights.color = new Color(0, 255, 0);// here I am accessing color and setting a new color value.

Suraj Bhandarkar
  • 346
  • 4
  • 11
0

In newer versions of unity (i'm using 2021.2.7f1) use this:

using UnityEngine.Rendering.Universal;

And then in code:

Light2D light = gameObject.GetComponent<Light2D>();

Snuiper228
  • 21
  • 4