Skip to content

Instantly share code, notes, and snippets.

@TheCuttlefish
Created February 26, 2026 16:33
Show Gist options
  • Select an option

  • Save TheCuttlefish/8fb810defdf612ac0eb61fa602ca3fc3 to your computer and use it in GitHub Desktop.

Select an option

Save TheCuttlefish/8fb810defdf612ac0eb61fa602ca3fc3 to your computer and use it in GitHub Desktop.
Remembers last button in Unity (keeps UI focused when you click off using mouse)
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class RememberLastButton : MonoBehaviour
{
GameObject lastSelected;
private void Update()
{
if(EventSystem.current.currentSelectedGameObject == null)
{
if (lastSelected && lastSelected.gameObject.activeSelf && lastSelected.GetComponent<Button>() != null && lastSelected.GetComponent<Button>().interactable)
{
EventSystem.current.SetSelectedGameObject(lastSelected);
}
}
else
{
lastSelected = EventSystem.current.currentSelectedGameObject;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment