- Home /
Add Light Source to Sun in Environmental Lighting with Unity 5 by Script?
I've checked all the API with Unity 5, and unless I'm missing something, I can't seem to find out how to add a light source to the Skybox Sun under the Lighting part of Unity.
Is there another way to go about this? How could I achieve this if there is no way to access it?
I would like to choose, by script and on start, what light source to add according to the light source added in the editor.
RenderSettings doesn't contain any sun related words. I have tried everything, unless it's accessed somewhere else.
If it's not possible, why would Unity not make this visible? It seems like something someone might want to have access to.
Any info would be greatly appreciated.
Not only is it not possible to set through scripting, the Sun isn't even saved when you save the scene.
Note: I did a bit of testing on the saving issue: a directional light in a prefab instance will not save, but a directional light NOT in a prefab instance will save properly.
Just submitted a bug report regarding this to Unity.
The sun in the default skybox is controlled by the direction of the directional light in the scene. You can see this in the editor by manually rotating the directional light.
I have a feeling I am misunderstanding the question though.
If the sun isn't set manually, it auto maps to the brightest directional light. If you have a day/night cycle with an additional directional light for the moon, then as soon as your moon is brighter than your sun the sky suddenly changes to day.
For basically any day/night sky system, we need to be able to set the sun from script. Blindly relying on the brightest directional light to be the sun only works during daytime.
Answer by NathanWarden · Jul 11, 2016 at 10:43 PM
This is what I did since it automaps to the brightest light so that it wouldn't break my day/night cycle.
1) Parented another directional light under my sun light and called it "DummyEnvironmentLight". 2) Zeroed out Position and Rotation values so it will always be oriented exactly the same as my sun light. 3) Cranked up the intensity to 8. 4) Set the culling mask to Nothing.
Unity picks it up as the sun even though it will never affect anything. It also works in a prefab.
Hope this helps somebody :)
Answer by Stormy102 · Aug 28, 2016 at 09:19 PM
Guys if you vote here we might be able to get this in 5.4.x! https://feedback.unity3d.com/suggestions/option-to-set-sun-light-via-script