I have a gameobject with a Horizontal Layout Group, and within in the layout group I have several (instantiated) gameobjects, each has a button component.
What I am trying to achieve is, when pressing one of them it becomes highlighted, and when pressing another, the first one losses its highlight, and the new pressed gets it.
I am trying to do it without having to save them all, and unhighlight them all whenever one of them is pressed.
And I told you exactly how to do that. You simply record which one you “highlighted” and then reference that variable again to turn it off when you click another. Then you just save out the new gameobject to the variable so you always know which one is highlighted.
private GameObject highlightedObject; //Keeps track of which gameobject has the highlight on
//Call a method to turn on the highlight
public void pressedGameObject(GameObject newObject)
{
if(highlightedObject != null) //Check first if null. If null, you had no object highlighted
//turn off highlight within this if statement. Depending on how you highlighted it.
highlightedObject = newObject; //Set the variable to whatever gameobject you clicked on and it should be the object that will now be highlighted
//Turn on highlight of newObject
}
I tried to comment the code a bit more. But the basic idea is you have say 4 items that can be highlighted. I click on #2. #2 is now stored in the variable highlightedObject and has it’s highlight on. Now I click on #4. First thing I do is turn off the highlight for #2 by referencing that variable(within the if check). Then I assign #4 to the highlightedObject variable and turn on its highlight.
And note I don’t know how you have your highlight setup. Not sure if you are using an addon, changing sprites, using a shader, etc. So I’m just assuming since you know how to add the highlight that you also know how to remove it. This code is just to show you how you’re tracking what is currently highlighted so you don’t have to iterate.
I understand now.
Basically just save the reference of the last clicked, and turn it off, turn on new, set him to the last clicked.
Since I am working with event system I’ll just start listening to the click event whenever I am the last, and turn off the listening when I am no longer the last. That way I only listen when I am the highlighted.