Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unity 5 Environment lighting - scripting

Discussion in 'Scripting' started by LennartJohansen, Apr 10, 2015.

  1. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,394
    Hi.

    How do I access the Sun light under the Environment Lighting tap in the Lighting window from script.
    I can change the Skybox material with RenderSettings.skybox but can not find any way to set the Sun light from code.

    Can anyone help me?
     
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    The sun is the directional light in your scene hierarchy.
     
  3. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,394
    Yes, if you have only one directional light. With more than one you need to select one. If I do this in the editor it works as I want it. I need to override this setting on scenes I load.

    Any way to access this in script?
     
  4. TwiiK

    TwiiK

    Joined:
    Oct 23, 2007
    Posts:
    1,729
    I was wondering the same thing yesterday. I was going to add a moon to my day night cycle and thus I wanted to change the assigned skybox-light through script when the sun set and the moon rose.
     
  5. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    True, this setting does not seem to be exposed in the RenderSettings API, like most of the other values there.
    If you have no override assigned, the skybox will use the strongest light in the scene. So as a workaround you could make the moon have more intensity than the sun when you need the skybox based on the moon.
     
  6. TwiiK

    TwiiK

    Joined:
    Oct 23, 2007
    Posts:
    1,729
    Yeah, I know. But I quickly discovered that showing the moon as just a light blob on the sky wasn't very pretty. :p So I need a way to place a moon texture in the correct spot. It's something I've put on hold for the time being.
     
  7. alan-lawrance

    alan-lawrance

    Joined:
    Feb 1, 2013
    Posts:
    360
    I'm finding this to be a pretty severe problem.

    I want to set a specific directional light for use, but if you set the 'Sun' in Environmental Lighting, it's not saved with the scene.

    A workaround to this not-saving bug could be to set the Sun through scripting, but that is not exposed in the API.
     
  8. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
  9. RealAspireGames

    RealAspireGames

    Joined:
    Dec 24, 2013
    Posts:
    265
    I am also looking for a solution, it really does not make any sense, why in the render settings I can set a skybox or change the fog value. But I can't add a simple light on run time which will be the sun. In the game I am creating the skybox looks terrible at night if I do not set a sun light in the render settings manually. I am hoping there is a fix to this soon, or a work around.
     
  10. Tudor

    Tudor

    Joined:
    Sep 27, 2012
    Posts:
    150
    The render settings may use the "main directional light" automatically, but the skybox does not. If you try to make it night by rotating the sun (main and only directional light which is also not assigned in the render settings), then the sky remains bright.

    Did anyone fix this? And why is unity not exposing that one reference? I find it extremely hard to imagine why that's not already there and why it may be hard to expose.
     
  11. Zireael07

    Zireael07

    Joined:
    Apr 27, 2016
    Posts:
    47
    Is there a bug report on that? Has this been fixed? I am having an issue with lighting and assigning sun via script would fix it...
     
  12. Stormy102

    Stormy102

    Joined:
    Jan 17, 2014
    Posts:
    495
  13. FreeFly90

    FreeFly90

    Joined:
    May 28, 2016
    Posts:
    177
    Any news about this? I just found out I really need to be able to set the sun by Script .-.
     
  14. NodeRaiderGames

    NodeRaiderGames

    Joined:
    Apr 8, 2014
    Posts:
    26
    They have multiple day night scripts/assets on the asset store they are fairly inexpensive
     
  15. ddf

    ddf

    Joined:
    Jul 9, 2011
    Posts:
    54
    Just want to chime in that this is a real bad problem.
    • We use prefabs to share sky objects across multiple scenes, so I can't set the Sun with the Lighting window because of the bug that prevents it from saving if it's in a prefab instance.
    • We use directional lights to view models in our pause menu that are configured to only light those models and can be brighter than the light in the scene that should be the Sun
    • We render some models in the scene using a CommandBuffer
    • CommandBuffers appear to always use whatever the current Sun is when lighting models drawn by them
    • So when the light in the scene is not as bright as the pause menu lights, all of the models in the scene are lit by the pause menu lights even though the pause menu lights have a culling mask that should exclude them
    There's no way to set the Sun from script and there's no way to force a CommandBuffer to use a specific light, so there doesn't appear to be a way around this other than breaking the prefab connects for all of the sky objects so we can save their lights as the Sun in the Lighting window. Obviously that really really stinks if we ever need to modify all of those sky objects in the future.