2

So, I am trying to build a menu's animations. I want all buttons but the pressed one shunted aside. But since writing a separate script for each button is less than elegant (and in case of dynamic buttons, impractical), I need to think of something else.

So, I want to make a script that will be hooked on several buttons. When any of them is pressed, the script is called, identifies which button called it, and excludes it from having its shunting animation used. For a pseudo-code description:

for (each button){
 if (pressed_button != this_button){
  run this_button's animation
 }
}

But there's a problem: I can't for the life of me figure out how to identify the pressed button! Any tips?

Thanos Maravel
  • 169
  • 2
  • 9
  • See #2 from the duplicate. It's doing this exact thing in a function and receives the button reference as argument – Programmer Sep 17 '18 at 20:20
  • Huh! That looks like it could solve my issue! Title and the list of implementations didn't make it easy to find, though. – Thanos Maravel Sep 18 '18 at 04:32

1 Answers1

1

You can add an OnClick event to your button which can call any public function in the script you point it to. You can also pass a single parameter to the function which can be a reference to the button, an int, a string, etc. With this, you could have a single function with a switch statement that handles the button presses based on what you pass it.

enter image description here

Keep in mind, when you add an event to the button component you will need to drag in a GameObject from your scene with the script attached to it in order to access its public functions. You cannot just drag the script itself directly into the OnClick event.

Alex Myers
  • 6,196
  • 7
  • 23
  • 39
  • 1
    You can also simply pass in a reference as value (in this case e.g. either the gameObject or any component of it). So you don't need no switch depending on a value. – derHugo Sep 17 '18 at 17:13
  • That sounds like a great option. Can you do this via the OnClick configuration in the inspector? I am not at my computer right now to check, but I don't recall seeing that before. – Alex Myers Sep 17 '18 at 17:24
  • 1
    Yes it just works the same for reference parameters as with value parameters. It is also worth to mention that the method may take maximum 1 parameter. – derHugo Sep 17 '18 at 17:28
  • Huh, I really like the parameter idea! Still curious how one could identify the script's caller ftom within the script, cause it has to be possible, right? :P but, your parameter aproach will be the one I'll use. – Thanos Maravel Sep 17 '18 at 17:49
  • @derHugo that would be great if I wanted just the pressed button to act, but I want the opposite. So i'd still end up having to filter it. – Thanos Maravel Sep 17 '18 at 17:53
  • @ThanosMaravel If you pass a reference to the button as derHugo mentioned, this should qualify as identifying the caller, right? – Alex Myers Sep 17 '18 at 17:57
  • @ThanosMaravel this is exactly what you are doing in your pseudo code example ... – derHugo Sep 17 '18 at 20:16
  • 1
    Well, yes, I mean, this solves my issue, and I consider the practical half of the question answered. But if there's a way for a script to identify its calling component on the sole principle of being its calling component (not because it feeds an "identification tag" but because the code can trace what calls what), then I want that too, for academic curiosity. It might come in handy someday. – Thanos Maravel Sep 18 '18 at 04:29