RenderSettings.ambientLight change not working! C# Scripting.
When I try to change RenderSettings.ambientLight gradually over time, it instead just flashes over and over. Even though I set the RenderSettings.ambientLight (which is a Color) to another new Color (ambientLightColor), it simply refuses to follow the same values and instead just flashes (rgb values go up and down).
ambientValue = ambientLight / 255f;
RenderSettings.ambientLight = ambientLightColor;
ambientLightColor = new Color(ambientLight / 255f, ambientLight / 255f, ambientLight / 255f, 0);
RenderSettings.ambientIntensity = 0;
ambientIntensity = RenderSettings.ambientIntensity;
Here is the whole script: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class env_sun : MonoBehaviour
{
[SerializeField] float cycleTime = 10f;
[SerializeField] float elapsedTime;
[SerializeField] float ambientLight = 19f;
public bool night;
float nightLight = 19f;
float dayLight = 100f;
float dst = 0;
[SerializeField] float spd = 1;
[SerializeField] float ambientValue;
[SerializeField] Color ambientLightColor;
[SerializeField] float ambientIntensity;
// Start is called before the first frame update
void Start()
{
dst = Mathf.Abs(nightLight - dayLight);
spd = dst / cycleTime;
}
// Update is called once per frame
void Update()
{
if (ambientLight > dayLight) night = true;
else if (ambientLight < nightLight) night = false;
if (!night)
ambientLight += spd * Time.deltaTime;
else
ambientLight -= spd * Time.deltaTime;
elapsedTime += Time.deltaTime;
if (elapsedTime >= cycleTime)
{
elapsedTime = 0;
}
ambientValue = ambientLight / 255f;
RenderSettings.ambientLight = ambientLightColor;
ambientLightColor = new Color(ambientLight / 255f, ambientLight / 255f, ambientLight / 255f, 0);
RenderSettings.ambientIntensity = 0;
ambientIntensity = RenderSettings.ambientIntensity;
}
}
And here is how it looks like, notice how the RenderSettings.ambientLight is changing (left) and how the ambient light color is supposed to change (right). What is happening? Help would be really appreciated, because this seems really confusing.
Answer by aylexofficial1379 · Apr 25 at 06:27 PM
Oh wow. After some time working on other scripts, I noticed I had 2 "env_sun" scripts in the scene. This caused this bug (because they had different Cycle Time). This seemed like it'd be hard to find if i had a ton of objects in the scene, but since I didn't, I just managed to stumble upon it.
The way to properly fix it though would have been to just search in the Hierarchy panel for "env_sun" and see if other objects pop up. Like how it's shown here: