Is it better to use Strings or Ints when making different types of a button?

More specifically, I have a script that has basic functions for all buttons like hovering, clicking etc. and then an action method that changes depending on a variable called “type”.
The type just indicates what button it is (like start game, settings, etc.). I just want to know if it would be better for me to indicate this with a string (like type = “Start Game” or type = “Settings”) or use an integer (like type = 0, type = 1)?

Neither. You ideally don’t want to be writing rigid code like that. If you want each button to do different things, hook the buttons callbacks to different methods instead.

1 Like

Unity has out of the box solutions that are 100X better than anything you’ll come up with at 100th the effort.

I don’t think I quite understand

Could you possibly give any of those out of the box solutions?

Use something like a serialised UnityEvent to simply reference a method and call that method.

uGUI buttons have this functionality built in. There really shouldn’t be any need to implement this yourself.

Unity has two built in UI solutions: Unity - Manual: Comparison of UI systems in Unity

1 Like

How would I know what method to call? I’m using prefabs and instantiating buttons onto the canvas, and I don’t know how to differentiate them in code so I know what method to call from the event.

I mean your opening post described what sounds like buttons for a pause or main menu UI, so why would you need to runtime instantiate buttons for something that is generally rigid? That kind of UI can be all be set up ahead of time as one piece.

In any case for runtime instantiated buttons, either they all do the same thing and you just subscribe a method to their callback, or if they do different things, that different functionality is present on the buttons themselves via different components.

All the buttons I use are for like starting the game, settings or accessing different menus. But, I just decided that the transitions Unity gives to buttons are too boring, so I decided to just make a script for custom transitions, as well as other things like sliding onto the screen or growing when they appear, and I don’t want to have to make a separate script for every single button so they can have unique effects.

I’m kinda new to Unity, so I don’t understand a lot of the terms like “subscribe” or “callback”.

Right, but why does that require you to instantiate them at runtime?

I guess not. But I still don’t know how I would differentiate between them in the script.

You don’t need to. Say you were using the existing UnityEngine.UI.Button component. You would just use their UnityEvent in their inspectors to reference different methods.

And you can still use the built in components, and add on your own components to add extra affects as well. No need to reinvent the wheel.

1 Like

Okay, I’ll try that out

1 Like