Highlight the last pressed gameObject.

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.

Thanks.

Just record the one you highlighted. Pretty much something like below.

private GameObject highlightedObject;

//Some pressed method
public void pressedGameObject(GameObject newObject)
{
if(highlightedObject != null)
//turn off highlight
highlightedObject = newObject;
//Turn on highlight of newObject
}

I’m sorry but I do not fully understand what you mean here.

Consider in hierarchy the following:
Screen → Layout → [GameObject, GameObject, GameObject, GameObject]

Now you press only one on of them.
That one is highlighted.

Now you press on another.
You want the old one to not be highlighted, and the new one to be.

So the problem is not how to highlight, but rather how to REMOVE the highlight from the other without iterating on all of the ones that exist.

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.

Thanks for the direction! It served me well.

It sounds like you are looking for RadioButtons.
You can achive that by using Toggles instead of Buttons and assign the same ToggleGroup to them.