Finding all sprite renderers in the scene and tell them to change color

I’m working on a game where you need to cycle through different map modes. When you change the map mode, all my sprite renderers in every region change color. I cant have a script for every region doing this because I will have a ton of regions.

This is the script that I thought might work:

using UnityEngine;
using System.Collections;

public class StateVariableAndMapmodeAffectsOnStates : MonoBehaviour {

	public GameObject[] stateDataArray;

	void Start () 
	{
		stateDataArray = GameObject.FindGameObjectsWithTag("Region");
	}

	void Update () 
	{
		foreach(GameObject go in stateDataArray)
		{
			GetComponent<SpriteRenderer>().color = Color.red;

		}
	}
}

This doesn’t do anything right now when I run the game. I need to make a script that finds all the sprite renderers in the scene and can constantly tell them to change color. If you know how I should change my script to do this, please tell me.

Also, should I be using an array here or a list? I will be having multiple variables factor in to how the sprite renderers change color, so I thought I should use an array.

You have probably solved your problem in 2 years, but here is for the archive…

You are not using go in your Update method.
It should be:

go.GetComponent<SpriteRenderer>().color = Color.red;

You can also add more tests to check if the SpriteRenderer component exists, if some objects tagged “Region” do not have a SpriteRenderer. I don’t know what your regions consist in exactly. Are they parents of the sprite objects? In this case, you should iterate on their children:

foreach (GameObject region in stateDataArray)
{
    foreach (GameObject child in region.transform)
    {
        // apply operation on child
    }
}

Or maybe you have a few different region parent objects associated with a few different colors? (with tags Region1, Region2, etc.)

In this case, you can reference the regions to some script in the scene in the editor, so that you don’t have to find them in runtime (that said, finding objects only on Start is fine).

If you have objects here and there and you want them to react to a specific signal, consider using C# events: Handling and Raising Events (Microsoft documentation) and C# Events in Unity

I guess you wrote Update() just for the example. Of course, you should change the color sprite just when you need it.