C# How do I approach? All gameObjects with Tag and this bool

[SOLVED]

This is what I have, works fine, but I need it to require that EVERY single gameObject with the tag “TileColor” has to have the bool “isBlack” true before it executes the rest of the script.

So, every single gameObject with Tag “TileColor”, must have their isBlack bool set to true before it works.

public class endCheck : MonoBehaviour
{

    public float delayLoad = 0.5f;


    void OnCollisionEnter(Collision collider)
    {
        if (GameObject.FindWithTag ("TileColor").GetComponent<ChangeColor>().isBlack)   //!GameObject for if it is false
        {
            print ("@@@ EndTrigger is Working @@@");
            Invoke ("WinFreeze", delayLoad);
        }
    }
    

    void WinFreeze()
    {
        Time.timeScale = 0;
    }

}

Thank you very much!

So first off dont use GameObject.Find so much, ever collision seems to be alot and GameObject.Find is super slow. Do something like this:

private ChangeColor[] cC;
public int amountCC;

void Start ()
{
while (amount > 1) //use a while loop to gain the amount of cC you have
{
amount--; //lower the ammount
cC[amount] = GameObject.FindWithTag ("TileColor").GetComponent<ChangeColor>(); //get the cC
}
}

Now if you have an array of data to sort through.

Lets use a foreach loop to check the state of cC.isBlack.

private int colorsNotBlack; //stores how many cC are not black

void OnCollisionEnter(Collider other)
{
colorsNotBlack = 0;
foreach (ColorChange item in cC) //loops through all the cC
{
if (!item.isBlack) //if they are not black then colorsNotBlack++
{
colorsNotBlack++;
}
}
if (colorsNotBlack == 0)
{
//they are all black
}
}

I hope this helps if you have any questions please ask. :slight_smile:

Hi RavenOfCode,

Ive been running into more trouble.

Ive taken what you wrote, not sure if the spots that stayed red and had [amount] needed a number or you meant amountCC.
Also not sure how to have this executed and make it work.

This is how I have it right now
To clarifiy, the tiles are either red, green,white or black (each with a seperate bool). they go from red to green to white then black. This all works so far.

Now, If all tiles are set to black, then Time.timeScale=0;

Here is how it looks atm:

using UnityEngine;
using System.Collections;

public class endCheck : MonoBehaviour
{
   
    public float delayLoad = 0.5f;

    private ChangeColor[] cC;
    public int amountCC;

    void Start ()
    {
        while (amountCC > 1) //use a while loop to gain the amount of cC you have
        {
            amountCC--; //lower the ammount
            cC[amountCC] = GameObject.FindWithTag ("TileColor").GetComponent<ChangeColor>(); //get the cC
        }
    }


    private int colorsNotBlack; //stores how many cC are not black

    void OnCollisionEnter(Collider other)
    {
        colorsNotBlack = 0;
        foreach (ChangeColor item in cC) //loops through all the cC
        {
            if (!item.isBlack) //if they are not black then colorsNotBlack++
            {
                colorsNotBlack++;
            }
        }
        if (colorsNotBlack == 0)
        {
            //they are all black
            Invoke ("WinFreeze", delayLoad);
        }
    }

    void WinFreeze()
    {
        Time.timeScale = 0;
    }

}

Oh sorry I forgot to clarify, amountCC is the number of cC you have in the game. So if you have 10 tiles with the ChangeColor script attached then just make that number 10. Also line 14 should be:
while (amountCC > 0) This way it gets the last one.

Hope this helps! :slight_smile:

I’m pretty sure that would assign the first GameObject with the tag to all the slots in the array. You want FindGameObjectsWithTag, which will return an array containing all said objects.

private List<ChangeColor> cC = new List<ChangeColor>();

void Start () {
  var gameObjs = GameObject.FindGameObjectsWithTag("TileColor");
  for(int i = 0; i < gameObjs.Length; i++) {
    cC.Add(gameObjs[i].GetComponent<ChangeColor>());
  }
}

This is assuming that everything tagged “TileColor” has a ChangeColor component. If not, you should check for that in the loop and either skip them or report an error as appropriate.

2 Likes

True, I suppose that was is better, or you could just change the name of the object after you get it. Yours is probably better though.

Thank you very much!

1 Like