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?

2
  • See #2 from the duplicate. It's doing this exact thing in a function and receives the button reference as argument Commented Sep 17, 2018 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. Commented Sep 18, 2018 at 4:32

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

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.
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.
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.
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.
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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.