Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Is it possible to set the Sun reference from Lihting/Scene/EnvironmentLighting through code?

Discussion in 'General Graphics' started by Tudor, Oct 6, 2015.

  1. Tudor

    Tudor

    Joined:
    Sep 27, 2012
    Posts:
    150
    In my projects I keep loosing the reference to the Sun (main directional light) in the Scene tab under the Lighting panel.
    I forget to check this, then make a build, and the testers keep reporting how when the sun goes down the sky stays bright (because the reference isn't there).

    So I'd like to set it through code (either at runtime or through an editor script).

    I noticed you can (re)assign the sun during runtime by hand. But through script I can't find anything under `RenderSettings`. I only found `RenderSettings.skybox`...

     
  2. Jordi-Bonastre

    Jordi-Bonastre

    Unity Technologies

    Joined:
    Jul 6, 2015
    Posts:
    102
    Hi @Tudor

    When a procedural skybox is used, you can use this to specify a directional light object to indicate the direction of the sun. If this is set to None then the brightest directional light in the scene will be assumed to represent the sun. If you flip off one bright light and enable another, then everything should adjust.

    http://docs.unity3d.com/Manual/GlobalIllumination.html
     
  3. Tudor

    Tudor

    Joined:
    Sep 27, 2012
    Posts:
    150
    That's right. I forgot about that. But actually I noticed that if you turn off the main (and only) directional light in the scene, and there is also a Point Light, the skybox will use that point light sometimes*(?) (and its z direction is where the sun is in the sky).

    I was seeing an odd sky with the sun just under my horizon and I thought it was due to a missing Sun; but it was a point light.

    *sometimes:
    -if you have just a directional light in your scene and you turn it off, the sky is black. If you then make a new point light in the scene, the sky remains black.
    -if you have a directional light in the scene and also a point light, and turn the dir light off, the point light becomes the sun.
     
  4. Feinoko

    Feinoko

    Joined:
    Sep 2, 2015
    Posts:
    4
    Hello,

    I have the same issue, it is game breaking for me, since each time I load/reload a scene, my Sun reference is removed, and it is messing with day/night cycle.

    Would anyone have any idea on how to set this "Sun" reference via script at runtime?


    Cheers!
     
  5. PeteUnity3D

    PeteUnity3D

    Unity Technologies

    Joined:
    Jan 4, 2016
    Posts:
    68
  6. Tudor

    Tudor

    Joined:
    Sep 27, 2012
    Posts:
    150
    Thanks Pete. Great to see you're responding to feedback pretty quickly. This is the second time I asked for a thing around here and then unity responded adding it in the following release.

    But you should also consider releasing the underlying Unity C++ code repository for free (like Unreal Engine has been doing since forever), because even though it could be considered a double edged sword, it would make a ton of community requests go away instantly, and there would be no angst waiting for the official implementations if devs can just do it themselves until then.
    Not to mention the potential for devs generating advanced new tech that you'd get "for free" when you decide to hire them for their engine mods ;)

    Sorry for the tangent, but to put it simply, the only reason I keep going back and forth between Unity and Unreal is that I can't write C++ in Unity.
     
  7. RealAspireGames

    RealAspireGames

    Joined:
    Dec 24, 2013
    Posts:
    265
    Just wondering if this has been added? This is actually making my night scene look pretty bad. I am using unistorm asset and making a multiplayer game, so each player has their own weather system also their own day & night cycles. Very disappointing I can not access this variable in the environment settings. Not game breaking for me, I just want my game to look nice.
     
  8. PeteUnity3D

    PeteUnity3D

    Unity Technologies

    Joined:
    Jan 4, 2016
    Posts:
    68
    Hi. The aim is to have this included in the main 5.4.0 release but currently isn't included in the beta releases of 5.4.0.

    Thanks,
    Pete
     
    RealAspireGames likes this.
  9. RealAspireGames

    RealAspireGames

    Joined:
    Dec 24, 2013
    Posts:
    265
    Thank you for the update, greatly appreciated. I can stretch my deadline until this feature to the engine has been added. I will simply work on other features in the game until then.

    Thanks again. :D
     
    PeteUnity3D likes this.
  10. Yukinohito

    Yukinohito

    Joined:
    Aug 25, 2016
    Posts:
    2
    Oct 9, 2016 Unity 5.4.0 still can't set sun object via script ...