J.K. Harthoorn
J.K. Harthoorn

Reputation: 218

If list contains 4 of the given gameObjects

I am trying to find how I can check if 4 of the given gameObjects are in a list. So I have a list with gameObjects. Sometimes a gameObject gets out of the list, sometimes it gets in. I want to check if 4 of the same colors are in a list. In this case, "childTiles" is the list. I tried it by 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. It does work if I check only 1 object. I also can't use ChildTiles.Contains() I think, because that is only possible with 1 gameObject if I'm right. So how do I check this with multiple gameObjects?

EDIT: A bit more information.

I have 4 different colors, each color has 4 tiles. Everytime the player clicks a button, the parent will rotate with the tiles as child. The childs are automatically child of a specific parent. The parents are the colliders (in the image below the white gameobjects are the colliders. They normally have no sprite renderer) which is behind the button that is pressed. The grey circles are the buttons.

https://i.gyazo.com/38fcf90e6bebe43763f5d358dd19f093.png

The following script is attached to all the white colliders. The public void Squareclicked is attached to each button, but it is in the 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 is where you do the rotation of the 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 the top-left, the top-right, the bottom-left and the bottom-right colliders are needed to check wheter is has four of the same color in it's list. I can either make a new script and attach that to those colliders (gameObjects) or I can make it so that the script only works for those.

Upvotes: 0

Views: 125

Answers (2)

Yair Halberstadt
Yair Halberstadt

Reputation: 6811

In answer to your request to explain how to use enums for colors here: I don't have unity, so I can't guarantee this will work, but anyway.

First, define your enum:

public enum Color
{
    Black,
    Green,
    Blue,
    Yellow,
    ///...
}

Next inherit a Tile object from GameObject (or whatever you want to use as a base class) and add a color property to it. Use these instead of GameObject as the Type of the ChildTiles.

public class Tile : GameObject
{
    public Color Color { get; private set; }
    public Tile(Color color) :base()
    {
        Color = color;
    }
}

Then your Update function should look something like this:

void Update()
{
        if (ChildTiles.Count(a => a.Color == Color.Green)>=4)
        {
            gameManager.finalGreenComplete = true;
            Debug.Log(gameManager.finalGreenComplete);
        }
}

Upvotes: 0

fuglede
fuglede

Reputation: 18201

The conditional will certainly never be true, since for a given tile, tile.gameObject.name has some given string value, and you are checking that every tile has each of the four names.

If I understand the question correctly, you want to check that within ChildTiles, there is some GameObject whose .gameObject.name is "tileGreen1", one that is "tileGreen2", and so on. If so, and if performance is not critical, you could check write the condition as follows:

var names = ChildTiles.Select(tile => tile.gameObject.name);
var shouldBeContained = new List<string> { "tileGreen1", "tileGreen2", "tileGreen3", "tileGreen4" };
bool condition = shouldBeContained.All(names.Contains);

Upvotes: 0

Related Questions