Image and score conditions

Does anybody know how to write the code to enable a score to increase if a specific image/sprite in an array is correct? Example, if button1 is pressed and if image/sprite is Image1/Sprite1, then score counter++. Help would be very much appreciated, Thank you.

You can probably check for the GameObject’s name in the array. Here is a post about something similar:

This seems helpful but I am struggling to connect the buttons and my sprites. I have a total of 4 buttons and 4 sprites(in array) but I can’t seem to link them together and trigger events if the conditions are met(i.e. button 1 is pressed and image1 is shown, then score increases). Could you please help me on this? Thank you.

I think i can help. There are most likely many ways to do this but the simplest way in my head (probably not the most efficient, but w/e) is to have a bool for each button. Like, isButtonOnePressed, isButtonTwoPressed, etc. Then you can have an EventTrigger on the button that uses OnPointerDown or OnPointerUp and when the button is pressed, that specific bool gets turned to True.

For the images, you could do the same with 4 bools but it seems like there will be only one image shown at a time. If that is the case, you could just do a comparison like

if(mySpriteImageArray[0].activeSelf && isButtonOnePressed == true)
{
    ++myScore;

}

That code is very rudimentary but hopefully gives you an idea of how to compare. You could set up a bunch of conditions in Update, use a Switch/Case statement or the best is to use an EventSystem.
Here is a great tutorial for an event system. Using these will help you down the road a lot if you dont already use them.

1 Like