Can't get lighting to work in my 2D game

Hi,

I am trying to configure a brightness slider into the settings menu of my game, and I am having trouble getting it to work. I have followed some tutorials and other troubleshooting guides, and I just can’t get it to work. Here is what I have:

I have a UI slider setup that will be my brightness slider, and I am trying to adjust lighting based on that. However, since my game is made up of sprites, I have added a material to each sprite that it set to sprite > diffuse. In the editor, I can get it to change while under Render Settings > Lighting. So I know I am close. But when adjusting the slider, my scene stays at the same brightness level. Here is my code so you can see what I have:

using UnityEngine.UI;

public Slider brightnessSlider;

private void Update()
    {
RenderSettings.ambientLight = new Color(brightnessSlider.value, brightnessSlider.value, brightnessSlider.value, 1);
}

I have my brightness slider set to be a range from 0-1. I don’t know why this isn’t working. It works in the editor when changing it directly in Render Settings, but when trying to do it through c# with a slider, it doesn’t work. Can someone tell me what I am missing or what I am doing wrong?

Thanks!

RenderSettings are per-scene, and only 1 scene’s settings are active at a time. My guess is that you adjust these settings in one scene, then load a new scene, thus loading the new scenes RenderSettings.

When you load a new scene in single-scene mode, it becomes the new active scene. When you load a new scene in additive-scene mode, the first scene is still the active scene.

I may be misunderstanding you, but I don’t think that is the case with my issue. I am just working on one scene now, and when I go to test the lighting, I simply press the play button to test it. I am not loading any new scenes and am just trying to get it to work in the one scene I am testing with currently.

The more I research it, I think it must be an issue with the code somehow. I can change the ambient lighting in the render settings editor and it works, but RenderSettings.ambientLight doesn’t seem to be working for me. Maybe it is an issue with 2D, not sure. I even tried RenderSettings.ambientLight = Color.red; since I know that should work, since that is the code from the unity documentation. Still doesn’t work. If anyone has any thoughts, I am grasping at straws here trying to get this to work.

Update: it started working on its own after updating Unity. So it must have been some sort of conflict.

However, I have a related question/problem: My UI overlay is unaffected by the lighting changes. The rest of the sprites are affected and change during runtime when I change my brightness slider, but the UI overlay doesn’t change when I move the slider, even though the sprites in the UI overlay have the Sprite Diffuse material attached.

Is there a reason that the UI overlay is unaffected by ambient lighting? Is there a way I can change this? Note: I read something about it possibly being due to Screen Space - Overlay, as opposed to being set to Screen Space - Camera, but I would really like to keep this on Overlay if possible.

Thanks for any help!

Sorry for not replying for so long, I was on vacation.

I’m not sure why UI wouldn’t be affected by ambient light… Canvases often don’t re-render their contents unless they detect something has changed. When you adjust your ambient light slider, try clicking the “next frame” button at the top of Unity, next to the “play” and “pause” buttons.

Also, on your canvas component, set “additional shader channels” to “everything”, just to be sure your vertices have enough data to support lighting.

Thanks for your reply. I tried setting additional shader channels to “everything”, and it didn’t do anything. I caved and decided to switch to screen space - camera, and now it is working properly. So the moral of the story is: if you need your UI canvas to be reactive to ambient lighting, don’t use screen space - overlay.