Unity 5 Environment lighting - scripting

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?

The sun is the directional light in your scene hierarchy.

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?

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.

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.

Yeah, I know. But I quickly discovered that showing the moon as just a light blob on the sky wasn’t very pretty. :stuck_out_tongue: 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.

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.

I just found this by googleing: Set DirectionalLight Rotation for Day/Night per Time - Questions & Answers - Unity Discussions maybe this is an approach you could go for?

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.

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.

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…

Guys this is on the feedback tracker - https://feedback.unity3d.com/suggestions/option-to-set-sun-light-via-script

Any news about this? I just found out I really need to be able to set the sun by Script .-.

They have multiple day night scripts/assets on the asset store they are fairly inexpensive

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.