Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

LWRP Light 2D change sprite in script

Discussion in 'General Graphics' started by Tiki720, Oct 1, 2019.

  1. Tiki720

    Tiki720

    Joined:
    Sep 10, 2017
    Posts:
    3
    I need to change the sprite of a 2D light in a script. I need to create a Light2D with a light type Sprite and then in the script change the sprite to whatever I need. I looked at the object and all I can see that is is a sprite is lightCookieSprite that is only get-able.

    Code (CSharp):
    1. GameObject glowComponent = Instantiate(new GameObject("GlowComponent"));
    2. Light2D light = glowComponent.AddComponent<Light2D>();
    3. light.lightType = Light2D.LightType.Sprite;
    4. light.??? = sprite;

    How can I change the sprite like in the inspector?
     
  2. gedadh

    gedadh

    Joined:
    Jun 3, 2019
    Posts:
    1
    Hi, did you find the way ?
     
  3. atk_defender

    atk_defender

    Joined:
    Jun 26, 2018
    Posts:
    20
    Same question.
     
  4. TyrantNomad

    TyrantNomad

    Joined:
    Jul 4, 2013
    Posts:
    5
    By editing your temporary version of Light2D.cs you can work around this; I'm not sure why it's not settable, Light2D itself already supports the sprite being changed in other parts of the code from what it seems.

    You'll need to change line 212 to the following:

    Code (CSharp):
    1. public Sprite lightCookieSprite
    2.         {
    3.             get { return m_LightCookieSprite;}
    4.             set { m_LightCookieSprite = value; }
    5.         }
    I'm not sure how safe or stable this is, but it works decently enough until Unity updates the field to be officially settable.
     
    atk_defender likes this.
  5. unity_mmH2Hk7-IqvJ7A

    unity_mmH2Hk7-IqvJ7A

    Joined:
    Aug 11, 2019
    Posts:
    1
    How do you access the Light2D.cs file? I havent been able to find it.
    Edit: Nevermind, You just gotta right click the component and hit "Edit script"...
     
  6. Chikari

    Chikari

    Joined:
    Aug 10, 2013
    Posts:
    3
    Can Unity please take care of this? This sound trivial to fix but it would save so much pain... fields visible in the inspector should be changeable by code.
     
  7. kiet57441

    kiet57441

    Joined:
    Mar 21, 2020
    Posts:
    1
    yea this work perfectly but i dont know why my script keep reset every time i turn on unity
     
unityunity