If I’m running this the _shouldchange bool will change like 5-10 seconds too late to false even if the Lerp is completed? how can I fix that?
public class ColorCycler : MonoBehaviour
{
public Color[] Colors;
public float Speed = 5;
int _currentIndex = 0;
Camera _cam;
public bool _shouldChange = false;
public float time;
public float CycleEverySeconds = 5f;
void Start()
{
_cam = GetComponent<Camera>();
_currentIndex = 0;
}
public void SetColor(Color color)
{
_cam.backgroundColor = color;
}
public void Cycle()
{
_shouldChange = true;
}
void Update()
{
time += Time.deltaTime;
if (time >= CycleEverySeconds)
{
Cycle();
time = 0;
}
if (_shouldChange == true)
{
var startColor = _cam.backgroundColor;
var endColor = Colors[0];
if (_currentIndex + 1 < Colors.Length)
{
endColor = Colors[_currentIndex + 1];
}
var newColor = Color.Lerp(startColor, endColor, Time.deltaTime * Speed);
SetColor(newColor);
if (newColor == endColor)
{
_shouldChange = false;
if (_currentIndex + 1 < Colors.Length)
{
_currentIndex++;
}
else
{
_currentIndex = 0;
}
}
}
}
}