Search Unity

Resolved can a function be stored in an array AND called using its index?

Discussion in 'Scripting' started by Deleted User, May 15, 2022.

  1. Deleted User

    Deleted User

    Guest

    i want to store some functions in an array and then call one of them using its index position in the array instead of its name. is that possible?

    thanks!!
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    Yes

    Code (csharp):
    1. private Action[] actions = new Action[3];
    2.  
    3. void Start() {
    4.     actions[0] = Foo;
    5.  
    6.     actions[0]();
    7. }
    8.  
    9. void Foo() {
    10.     Debug.Log("Foo got called");
    11. }
    See delegates.
     
  3. Deleted User

    Deleted User

    Guest

    THANKS!
    it worked!