Hotbar button shading flickering

Hi everyone, I’m trying to make a simple hotbar. Let’s say there are three hot bar buttons whose default color is gray. When a player presses the number 1 key on their keyboard, the first button changes to a yellow color and stays a yellow color until one of the other buttons are pressed. In that case, the initial first button reverts back to its default gray color and the button that was pressed turns yellow.

It seems simple but I’m having trouble implementing it in my scripts. In my implementation right now, when a user presses 1, it flashes the “Pressed Color” and then transitions to the “Highlighted Color” and stays in the “Highlighted Color” state. When another number key(i.e number key 2), it does the same behavior and the number key 1 button stays on so now I have two buttons highlighted.

I’m stumped on how to implement what I’m trying to do. Anyone got any tips?

Here is my code for the second button which I copy and pasted and made minor changes to the first button:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; //Namespace required for Button objects.
using UnityEngine.EventSystems;

public class Button2 : MonoBehaviour
{

public Button button2;


void Start()
{

}

// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Alpha2))
{


 
button2.onClick.Invoke();
var go = button2.gameObject;
var ped = new PointerEventData(EventSystem.current);
ExecuteEvents.Execute(go, ped, ExecuteEvents.pointerEnterHandler);
ExecuteEvents.Execute(go, ped, ExecuteEvents.submitHandler);
}

}


}

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: Using code tags properly

You may edit your post above.

Generally break the process down into stages:

  1. gather user intent

  2. process user intent and decide how to:

  3. change state of game

Step 3 above will often involve adjusting the UI state to reflect the change, such as graying it out for a cooldown period.

Cooldown timers, gun bullet intervals, shot spacing, rate of fire:

GunHeat (gunheat) spawning shooting rate of fire: