c# - If list contains 4 of the given gameObjects -
i trying find how can check if 4 of given gameobjects in list. have list gameobjects. gameobject gets out of list, gets in. want check if 4 of same colors in list. in case, "childtiles" list. tried doing this:
void update() { foreach (gameobject tile in childtiles) { if (tile.gameobject.name == "tilegreen1" && tile.gameobject.name == "tilegreen2" && tile.gameobject.name == "tilegreen3" && tile.gameobject.name == "tilegreen4") { gamemanager.finalgreencomplete = true; debug.log (gamemanager.finalgreencomplete); } } }
this returns nothing. work if check 1 object. can't use childtiles.contains()
think, because possible 1 gameobject if i'm right. how check multiple gameobjects?
edit: bit more information.
i have 4 different colors, each color has 4 tiles. everytime player clicks button, parent rotate tiles child. childs automatically child of specific parent. parents colliders (in image below white gameobjects colliders. have no sprite renderer) behind button pressed. grey circles buttons.
the following script attached white colliders. public void squareclicked attached each button, in same script.
public list<gameobject> childtiles = new list<gameobject>(); public void squareclicked() { if (parentpositions.canturn == true && gamemanager.turns > 0f) { gamemanager.turns -= 1; gamemanager.turnstext.text = "turns: " + gamemanager.turns; foreach(gameobject go in parentpositions) { parentpositions = go.getcomponent<tilecontroller> (); parentpositions.canturn = false; } foreach(gameobject tile in childtiles) { tile.transform.parent = gameobject.transform; } startcoroutine(rotate()); //this rotation of parent object. if (gamemanager.turns == 0f) { gamemanager.turnstext.text = "no turns left"; } } } void ontriggerenter2d(collider2d col) { if (col.gameobject.tag == "tile") { childtiles.add (col.gameobject); } } void ontriggerexit2d(collider2d col) { if (col.gameobject.tag == "tile") { childtiles.remove (col.gameobject); } }
only top-left, top-right, bottom-left , bottom-right colliders needed check wheter has 4 of same color in it's list. can either make new script , attach colliders (gameobjects) or can make script works those.
the conditional never true, since given tile
, tile.gameobject.name
has given string value, , checking every tile has each of 4 names.
if understand question correctly, want check within childtiles
, there gameobject
.gameobject.name
"tilegreen1"
, 1 "tilegreen2"
, , on. if so, , if performance not critical, check write condition follows:
var names = childtiles.select(tile => tile.gameobject.name); var shouldbecontained = new list<string> { "tilegreen1", "tilegreen2", "tilegreen3", "tilegreen4" }; bool condition = shouldbecontained.all(names.contains);
Comments
Post a Comment