Best Practices for buttons?

Hello All First time poster. okay programmer but stilling learning my way around unity and c# and still figuring out best practice.

When using UI buttons for something like a difficulty selection I can think of a few different methods for handling it for example:

  1. Create a function that assigns the difficulty variable for each button and assign it to the onclick event for the button.
  2. Create a single function with if statements that checks the button pressed and assigns the difficulty variable based on that.

however I’m not sure which of these would be considered the better method for handling something like this or what would be considered better design?

Hiya!

Well, I think it would depend on what exactly you want the difficulty settings buttons to do, but let’s say you have three buttons: Easy, Medium, Hard. I think the best way to do it would be to have one function for this maybe called SetDifficulty that takes in a number or something. Each difficulty button would then just use that function and give it a different number. If you were to have maybe two buttons that increment the difficulty, I’d suggest having two functions then to, well, increment and decrement the difficulty. Have each button use one of the two functions.


So, I guess if you have buttons that set the difficulty to something specific (like Easy, Medium, or Hard), I’d say to use one function, but you could probably find a way to do it without a bunch of if statements and use a parameter instead.