- Home /
Is it possible to reuse a script, but changing an event subscription?
Hi, I'm a newbie on unity and programming so maybe it's a silly question. To clarify here's an example:
Let's say I have a monobehaviour script named "ScaleAnimation" that has a method "ChangeScale" that changes the scale of the transform.
So I attach the script to "gameObject A" and then subscribe the method to "OnLeftClick" event. So far so good.
Now I attach the same script to "gameObject B" but I want to subscribe "ChangeScale" to "OnRightClick" event instead. Is that possible?
Can I select the event that the method is subscribed to, depending on which game object the script is attached to? Or do I need to duplicate the script, change it's name, re-subscribe it, and then attach it to gameObject B?
Answer by TomArano_Dimenco · Aug 05, 2020 at 09:34 AM
do you need to be subscribing to events?
Could u not just use a variable keycode (u can change this through a manager script easilly)
public Keycode keytouse;
void Update()
{
if (Input.GetKeyDown(keytouse))
{
//DO SOMETHING
}
}
//other script
ScaleAnimation.keytouse = keycode.Mouse0;
Thanks for the answer, I didn't know that. But if the events were OnPlayerDied and OnEnemyDied instead of an input key, is it possible?