- Home /
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);
}
}
Answer by Legend_Bacon · Nov 27, 2018 at 02:07 PM
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
Thanks for looking out on my question but the problem in this is, it destroys and change the colors of all the game objects I only want that to happen on the ones that has been clicked
Your answer
Follow this Question
Related Questions
Make a gameobject inactive and active 0 Answers
Object color change 0 Answers
Change the button target image alpha 1 Answer
Why is door opening so fast with boolean condition? 0 Answers
Unity GUILayout Button 3 Answers