- Home /
Selecting an object on click, then clicking a UI button to change its color.
I've been searching around everywhere trying to figure out how to do this, but I am honestly clueless. What I have is a bunch of "cards" and four colored buttons, as seen below:
This is set up to fit on my iPad. What I'm trying to do, essentially, is be able to select (and highlight) one of the 25 white cards when it's touched/clicked. Then, by pressing one of the four buttons – red, blue, brown, or gray – the card will change to the color of that button, also un-selecting (and un-highlighting) the card.
Hopefully that makes sense. I just haven't figured out how to do it yet. Any suggestions would be greatly appreciated. Thank you!
Answer by TheDJBuntin · Jul 03, 2017 at 07:47 PM
I'm sure there's a better way but quick answer: have a global class script that will store the currently selected object:
public static class G_Variables
{
public static GameObject g_SelectedObject;
}
and then assuming you've already made all the cards buttons have the OnClick function that is called for them do this
public void SomeOnClickFunction()
{
G_Variables.g_SelectedObject = gameObject;
}
finally, setup the OnClick functions for each of the buttons at the top to do whatever you want them to do, ie:
public void SomeOtherOnClickFunction()
{
if(G_Variables.g_SelectedObject) // Check to make sure something is selected
G_Variables.g_SelectedObject.GetComponent<Image>().color = Color.red;
}
Thanks so much for your help! I managed to get it working using your suggestions.
Your answer
![](https://koobas.hobune.stream/wayback/20220612124608im_/https://answers.unity.com/themes/thub/images/avi.jpg)