In c# this works:
Dictionary<int, System.Action> functions = new Dictionary<int, System.Action>();
I’m using System.Action because my functions return nothing and take no parameters
void Function1()
{
Debug.Log("This is Function1.");
}
//add a function to the dictionary with key 1
functions.Add(1, Function1);
//call it
functions[1]();
// console output:
//"This is Function1."
However, in a unity c# script I’m getting an invalidkey error. What am I doing wrong?
EDIT: I figured out what was wrong with my code. I’m using a getter and setter, and in the set block I wasn’t setting the variable to the keyword “value”
private int _funcNum
public int functionNumber
{
get
{
return _funcNum;
}
set
{
_funcNum = functionNumber; //This should be: _funcNum = value;
DoFunction(_funcNum);
}
}