How to trigger a "generic action" that can call different methods ?

Hi guys !
How can I manage to call different methods with same button ?

I mean, Fire2 should trigger the “active skill” but skills can have a very different behaviour ?

There are lots of posible answers to this. The best solution highly depends on what you are trying to generally accomplish, and on your approach to get there. And every possible answer will bring with it quite a few things to research in order to get going with it.

Start off with defining what a “skill” is, from a technical point of view. You somehow have to teach Unity what a skill is so you can define that there’s multiple of them and one of them is “active”. One solution that is pretty fancy (and not as confusing as it used to be in the past) are ScriptableObjects. A ScriptableObject is basically™ an asset that you program yourself.

Here’s an example:

using UnityEngine;

[CreateAssetMenu(fileName = "Skill", menuName = "ScriptableObject/Skill")]
public class Skill : ScriptableObject
{
    public int damage;
}

As you can see, this is not a MonoBehaviour and thus not a component you can slap on a GameObject. Instead, thanks to the attribute on top of the class, you can right-click in your project view, hit Create, ScriptableObject, Skill. This will create a new skill in your assets, just like you can create a new C# script there.

When you click your skill, you can rename it and set values for its properties in the inspector. The rules that apply are the same for properties in MonoBehaviour: All the public variables are exposed in the editor and saved in the project.

Next, you can create a regular MonoBehaviour script that utilizes skills:

using UnityEngine;

public class PlayerSkills : MonoBehaviour
{
  // An array of skills
  public Skill[] skills;
  public int activeSkillIndex;

  void Update()
  {
    if(Input.GetButtonDown("Fire 1"))
    {
      var skill = skills[activeSkillIndex];
      Debug.Log(skill.name + " deals " + skill.damage + " damage.");
    }
  }
}

Note how the damage value that has been defined in the ScriptableObject class and got a value from the editor can easily be accessed here. You can now change “Active Skill Index” in the editor and see how pressing the button triggers a different skill each time (assuming you have more than one skill created and added to the list in the component).

Remember that this is one possible solution. Depending on what you are doing and how you are doing it, it might not be the perfect one for you.

Just use classic OOP. Either define a base class from which each skill class is derived or define an interface which each skill class implements. An interface is usually the better approach:

public interface ISkill
{
    void TriggerAction();
}

public class SomeSkill : MonoBehaviour, ISkill
{
    public void TriggerAction()
    {
        // Trigger this skill's action
    }
}

public class AnotherSkill : MonoBehaviour, ISkill
{
    public void TriggerAction()
    {
        // Trigger this skill's action
    }
}

Now you can store your “current skill” in a variable of type “ISkill”. You can simply do

ISkill currentSkill;

void TriggerSkillAction()
{
    if (currentSkill != null)
        currentSkill.TriggerAction();
}

You can assign any class that implements the ISkill interface to the “currentSkill” variable.