How to change color on multiple game object when button is clicked

I have a multiple button that has an image in it, whenever a button is being clicked it changes color, but I have a condition to test and I also want to change the color of the Image attached to the Button objects when the condition is false. But if the condition is true I want to destroy the all the clicked Button Objects and Instantiate new ones. How would I properly do that? I have tried so many ways, but it all doesn’t work.

The Condition on my game manager script:

public void checkword()
    {
        wordBuilded = displayer.text.ToString();

       if (txtContents.Contains(wordBuilded))
       {     
            Debug.Log(wordBuilded + " Is on the list");
            FindObjectOfType<LetterTiles>().validWord();
       }
       else
       {        
           Debug.Log("Someting is wrong..");
           FindObjectOfType<LetterTiles>().invalidWord();
        }
    }

The script attached to every Button object:

using UnityEngine;
using UnityEngine.UI;


public class LetterTiles : MonoBehaviour {

    public string letter;

    public GameObject tile;

    Color32 tColor = new Color32(255, 215, 86, 255);
    Color32 origColor = new Color32(255, 250, 210, 255);

    public void letterClick()
    {
        gameManager.currentWord += letter;
        gameObject.GetComponent<Image>().color = tColor;
    }

    public void invalidWord()
    {
        gameObject.GetComponent<Image>().color = origColor;        
    }
    public void validWord()
    {
        Destroy(gameObject);
    }

    

}

Hello there,

The immediate way to make this work would be to use FindObjectsOfType(), instead of FindObjectOfType() (note the extra “s”).

This returns an array of all enabled objects of your chosen type in the scene. Once you have that, all you have to do is iterate through it and call the appropriate function.

LetterTiles[] tilesArray = FindObjectsOfType<LetterTiles>();

foreach (LetterTiles item in tilesArray)
{
      if(InsertYourConditionHere) // Is the word valid or not?
           item.validWord();
      else
           item.invalidWord();
}

Something like this should do it.


I hope that helps!

Cheers,

~LegendBacon