Update Color on ALL sprites simultaneously

HI,

I have a 2D game and I want to have a day/night cycle. I have it working but performance is slow. I have this script attached to ALL my sprites, so that they get brighter and darker based on the suns position and i would like to add a cloud cover adjustment as well. right now there are about 30 sprites in the scene.

Is there a way I can constantly update the brightness on everything without slowing things down?

Thanks!!

public class Shadow: MonoBehaviour {

	
	public float sunPosition;  // starting position is 5.6
   	GameObject oSun;
	float RGB;
	public float minLight = 0.05f;
public float startLight = 5.9f;

	
	void Update () {

				
		oSun = GameObject.Find ("sun");
		SpriteRenderer renderer = GetComponent<SpriteRenderer> ();
	sunPosition = oSun.transform.position.y;

		
		RGB = (sunPosition - startLight);

		if (RGB < minLight)
			renderer.color = new Color (minLight, minLight, minLight, 1f);
		if (RGB > minLight && RGB <= 1.0f)
			renderer.color = new Color (RGB, RGB, RGB, 1f);

				
		}
}

Generally, you never want to put GameObject.Find in an Update loop. It’s super slow.

Instead, move this to Start:

SpriteRenderer myRenderer;
GameObject oSun;
private void Start()
{
  oSun = GameObject.Find ("sun");
 myRenderer = GetComponent<SpriteRenderer> ();
}

And delete the same line in Update. I’d also recommend doing the same thing to your SpriteRenderer line.