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.

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

Discussion in 'Scripting' started by nunogoblin, May 15, 2022.

  1. nunogoblin

    nunogoblin

    Joined:
    May 20, 2021
    Posts:
    11
    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:
    5,844
    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. nunogoblin

    nunogoblin

    Joined:
    May 20, 2021
    Posts:
    11
    THANKS!
    it worked!
     
unityunity