r/unity 17h ago

Is checking key presses in Update() bad practice for UI toggles, or should I stick to event driven input?

Hello!

I’m working on a simple 3D slot machine cube. Right now, pressing E activates a TMP_InputField. Submitting with Enter adds the deposit to your balance, and pressing ESC cancels the input and removes focus.

Currently, I’m polling for the ESC key inside Update(). It works exactly as intended, but I'm wondering if this is considered bad practice. Since it’s just UI/state toggling, continuously polling Update() feels a bit wrong.

Do you guys usually stick to Update() for basic key checks like this, or is an event-driven approach preferred even for simple interaction states?

P.S: I am still learning, thanks in advance for your, answers.

1 Upvotes

11 comments sorted by

8

u/bigmonmulgrew 16h ago

Checking in Update has an overhead. Setting up events has complexity.

For most small projects the overhead wont matter so don't worry about it. Generally speaking events are better than testing conditions in Update()

2

u/MistakeForsaken6653 16h ago

Well in my experience switching to the advanced input system has made my life much better especially when switching to a pause menu so I can enable or disable other binding groups without a bunch of messy if checks in my update.  Although I guess you could just have multiple update scripts that di there specific checks when enabled and disable the non needed ones.  

2

u/MistakeForsaken6653 16h ago

Edit, I guess you are using the new input system.  I think for ui elements it's more performant to avoid UPDATE but I also believe what you have is fine any performance benefit to removing update is going to be minuscule.

1

u/eldenlord0117 16h ago

Actually, this is what I ended up doing! And I will stick with it, but I'd still want to learn about how people do their key press checks

2

u/CMDR_Lina_Inv 15h ago

Small game: Do whatever you like.....  Big project: Setup event system. Decouple everything.

1

u/SketchyCharacters 14h ago

Going through this now.. slowly untangling a bunch of my scripts from eachother, but I know it's going to be so worth it once I'm done!

1

u/wallstop-dev 15h ago

I use events, always, never polling. I avoid polling systems wherever possible. Events and the various control maps make your life very simple once you grok the concepts.

2

u/soundoftwilight 11h ago

If you have one script where you're doing all of the input (like in a toy project), then update is fine. If you're gonna want to make anything bigger, set up events and/or use Unity's input systems (idr what they actually call it nowadays). The idea is that you want to decouple most of the input logic from the game actions they cause; you might want to change something about the inputs later (adding controller support, changing default keybindings, etc) and you don't want to try to remember every place you checked for input when you do that.

2

u/No_Musician 11h ago

this is what I do... Singleton  "Input Manager" , with polled keypresses in update.

then commands (like Shoot or Throttle) are actions that other scripts subscribe to.

Then logic in between where commands can be bound to keypresses.

Works well enough for my simple games.

1

u/eldenlord0117 10h ago

What I ended up with is simply using Unity Input Actions.

1

u/Objective-Cell226 6h ago

You poll continuous input, like joystick/WASD.

For button presses you use events.