ATTENTION
Due to c# internal structure regarding actions and scopes, you have to take a safe step when you use SuperInvoke with parameterized methods.
The following examples show the issue:
int amount = 100;
SuperInvoke.Run(()=> withdraw(amount), 1f);
amount = 500;
How much will be actually withdrawn?
The answer is 500 because the variable amount was changed before the delayed method withdraw was actually executed.
Essentially, the method withdraw can only know the last value of the variable amount.
For loops are also affected by the c# limitation:
for(int i = 1; i <= 3; i++) {
SuperInvoke.Run(()=> Debug.Log(i), 1f);
}
The log output is:
3
3
3
It always logs “3” because the delayed method (Log in this case) can only know the last value of i, which was 3, when SuperInvoke.Run was actually executed (after 1s in this case).
To overcome the c# limitation you need to specify a new variable inside the body of the loop in which you assign the current loop value of the variable i :
for(int i = 1; i <= 3; i++) {
int k = i;
SuperInvoke.Run(()=> Debug.Log(k), 1f);
}
The log output is:
1
2
3