- Home /
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:
Answer by wibble82 · Dec 23, 2015 at 10:38 AM
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! :)
-Chris
I suppose I could ditch either the blue or red and just have one or the other and use the false as the equivalent for the other... Idk I just start duplicating to label things to spare my sanity, but it only seems to have made the situation worse. I will definitely be reading up on C#, in the meantime though, I'm on a time constraint. Thank you, I'll see if I can get this working.
Okay, yeah, this won't work because of the way I have it set up,
that being that I have a "Scout $$anonymous$$ovement, Fighter $$anonymous$$ovement, and Frigate $$anonymous$$ovement" set of scripts that contain individual "$$anonymous$$oveLim" ints...
but I think it gives me a better understanding of what I have to do to get it working. Thanks again.
Just have to switch a few things around.. and finagle it a bit. Have a happy new year!
Answer by corn · Dec 23, 2015 at 04:30 PM
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 !
Your answer

Follow this Question
Related Questions
Making an animation stop once it finishes (trouble with booleans) 1 Answer
how do i make a loop for a boolean? 2 Answers
Making a bool stay true for a second/while animation is playing 2 Answers
Not all code paths return a value in boolean loop 2 Answers
How can I do a foreach loop for an array of booleans? 1 Answer