How can I use a foreach loop with booleans linked to many game objects?

I’m making a Turn Based Strategy game, and each player has 3 “ship uses” meaning that they can move three units before it switches to the other player’s turn.

There are a few different unit types in this game so I gave each of them different “Move limits” to determine how far each unit can move if a player uses a ship use on it, by clicking it. (MoveLim in the code)

Each unit is a separate game object and therefore has its move limit kept track of individually as the script for tracking moveLim is attatched to each individual game object.

The turn system I created relies on the player turns switching after every few ship uses, so I was trying to make it so that when red player finishes with their turn, blue player’s moveLim on all its units is reset to 0.
Then, when blue player finishes THEIR turn, it would reset all of red player’s moveLim on all of the red units back to 0.

The issue is, although I can keep track of all the ship uses, I cannot find a way to reset the moveLims on multiple objects. I thought maybe foreach loops were the answer so I tried this…

void Update()
{

	if (shipUse > 3) 
	{
		if(redTurn)
		{
			redTurn =false;
			shipUse = 0;
			BlueTurn.SetActive (true);
			RedTurn.SetActive (false);
			Debug.Log("Red Turn Is Over");
			GameObject.FindObjectsOfType(blueTeamMembership);
				foreach(GameObject bluey in BlueTeamScript)
				moveLim = 0;

		}
		else
		{

			redTurn = true;
			shipUse = 0;
			RedTurn.SetActive (true);
			BlueTurn.SetActive (false);

			GameObject.FindObjectsOfType(redTeamMembership);
			foreach(GameObject redey in RedTeamScript)
				moveLim = 0;

		}					
	}
}

but to no avail…
Just this error:
Assets/Scripts/ShipUseCounter.cs(42,44): error CS1502: The best overloaded method match for `UnityEngine.Object.FindObjectsOfType(System.Type)’ has some invalid arguments

I also have a script that designated red team membership and blue team membership as true, attached to respective objects of each player. These are the bools I’m looking for.

Am I just misunderstanding foreach loops?

The RedTurn and BlueTurn set actives just make images in the scene appear that let the player know whose turn it is.

I’ve been racking my brain on this for quite some time. I’m not great at C# but I have no choice at this point.

Possibly Important Info:
A player clicks a unit and it becomes “selected” from there they use the arrow keys to move it from space to space, each movement from one space to another increases the moveLim of the specific unit, until it hits the cap of moveLim for that unit type.

The game is grid based, looks like this:

Your use of foreach and FindObjectsOfType is not entirely correct.

void Update() 
{
     //I am assuming this check is working
    if (shipUse > 3) 
    {
        //this could probably be done a little neater by storing the current player 'index', and
        //the player states in a 2D array, so we don't need to duplicate all our logic. for now
        //though, that's not your problem so we'll leave it with 2 if statements
        if(redTurn)
        {
            //I suspect this stuff to set the turn, reset ship use, and change the 'active' objects are fine
            redTurn =false;
            shipUse = 0;
            BlueTurn.SetActive (true);
            RedTurn.SetActive (false);
            Debug.Log("Red Turn Is Over");

            //now we need to find all objects that contain your 'ship' script. 
            //I'm going to assume you have a type (script) called 'BlueTeamScript' we do:
            
            //first we call FindObjectsOfType, passing in the 'type of BlueTeamScript'.
            //this returns an array of objects, which we have to cast to an array of 'BlueTeamScript[]'
            BlueTeamScript[] blueScripts = (BlueTeamScript[])GameObject.FindObjectsOfType(typeof(BlueTeamScript));

            //now we can use a foreach loop to iterate over all the scripts in the array
            foreach(BlueTeamScript blueScript in blueScripts)
            {
                //assuming the script has a public 'moveLim' property, we can now set it to 0 for
                //the current entry in the array
                blueScript.moveLim = 0;
            }
        }
        else
        {
            //blue is similar!
        }                    
    }
}

A big improvement (or way to avoid bugs) that you only have 1 ‘ship’ script, and store a bool on it that says whether it is a blue team or a red team ship. You need to be looking at minimizing duplicate code!

In general, I’d advice (just for your own sanity!) that you do a bit more reading on c# - maybe grab a book on it or something! My example above should work, but it’s really important to get a solid understanding of things like for loops and arrays, and I probably can’t do a good enough job of explaining them here. That code should work, but understanding why it works it’s really important and will be much better in the long run!

Good luck! :slight_smile:

-Chris

You’re not using neither FindObjectsOfType nor foreach correctly. I assume blueTeamMembership is a variable, hence the error you encountered, FindObjectsOfType needs to be called with a type. It seems like you’re looking for all instances of BlueTeamScript, so the way to do it is :

BlueTeamScript[] blueTeamMembers =  GameObject.FindObjectsOfType<BlueTeamScript>();

Since the result of FindObjectsOfType is now assigned to blueTeamMembers, you can iterate on that array and do whatever operation you need on all these BlueTeamScript instances :

foreach(BlueTeamScript bluey in blueTeamMembers)
{
    bluey.moveLim = 0;
}

Hope that helps !