Search Unity

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?
     
    UsefulYdyot likes this.
  2. gedadh

    gedadh

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

    atk_defender

    Joined:
    Jun 26, 2018
    Posts:
    25
    Same question.
     
    UsefulYdyot likes this.
  4. TyrantNomad

    TyrantNomad

    Joined:
    Jul 4, 2013
    Posts:
    6
    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:
    3
    yea this work perfectly but i dont know why my script keep reset every time i turn on unity
     
  8. unity_uNEXptjrFwHPwA

    unity_uNEXptjrFwHPwA

    Joined:
    Jan 8, 2021
    Posts:
    1
    Have u solved it? I have the same problem every time I open unity it resets
     
  9. xskyhigh12

    xskyhigh12

    Joined:
    Nov 18, 2018
    Posts:
    14
    I have the same problem
     
  10. dis_da_moe

    dis_da_moe

    Joined:
    Dec 8, 2020
    Posts:
    1
    Code (CSharp):
    1.     private Light2D _light2D;
    2.     private FieldInfo _LightCookieSprite =  typeof( Light2D ).GetField( "m_LightCookieSprite", BindingFlags.NonPublic | BindingFlags.Instance );
    3.  
    4.     void UpdateCookieSprite(Sprite sprite)
    5.     {
    6.         _LightCookieSprite.SetValue(_light2D, sprite);
    7.     }
    You can access the private backing field for the 'LightCookieSprite' property and set that. This applies for any other property that doesn't have a setter.
     
  11. GiomGots

    GiomGots

    Joined:
    Apr 3, 2021
    Posts:
    13
    I really don't know how that magic is possible but it works !
    Thanks !!