Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Choosing abilities based on inspector input

Discussion in 'Scripting' started by Globber, Aug 3, 2017.

  1. Globber

    Globber

    Joined:
    Feb 9, 2017
    Posts:
    8
    So i have some functions called ability1,ability2 and i was wondering can i choose which abilty gets chosen with a public int that is changed in the inspector and it determines which ability gets called. For example if that public int = 1 then the ability1 function gets called?
     
    Last edited: Aug 3, 2017
  2. kietus

    kietus

    Joined:
    Jun 4, 2013
    Posts:
    54
    Hello,

    If there are few cases, i think you can go with a Ability wrapper function.
    Code (csharp):
    1.  
    2. function void Ability(){
    3.    switch (abilityID)
    4.    {
    5.       case 1 : Ability1();break;
    6.       case 2 : Ability2();break;
    7.       ...
    8.    }
    9. }
    10.  
    For testing purpose, i guess that's ok.

    For more complex case, you could also use reflexion to call a method by it's name. (example) , or take a look at delegate in C#.