a quick way to check if all 6 buttons have the same sprite
In the case of tic tac toe, you can simply check if button [1].getcomponent ().sprite == ☆ && if button [2]...... for the three sprites to make a win condition. What if i am checking 6 buttons images?
Is there a better way instead of a long if ( " && " && " &&" ...etc.
A way of checking if all buttons within a list have the same sprite ir component.
Answer by Hellium · Jan 23, 2020 at 02:42 PM
Code not tested.
private bool AllButtonsShareSameSprite( params Button[] buttons )
{
Sprite sprite = (buttons[0].targetGraphic as Image).sprite;
for(int i = 1 ; i < buttons.Length ; ++i)
{
if((buttons[i].targetGraphic as Image).sprite != sprite)
return false;
}
return true;
}
Then call it like this: if(AllButtonsShareSameSprite(button)) // Do something
Thank you for the insight. Seems much simpler than what ive been trying.
Your answer
![](https://koobas.hobune.stream/wayback/20220612222008im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
checking if gameobject exists 1 Answer
Multiple objects writing to a single variable on another object's script. 1 Answer
Instantiated Child doesnt render if multiple objects share same name as parenting object? 1 Answer
Image Tracking with Vuforia 0 Answers
Object importing position and tranform of a bone during animation 1 Answer