Dani Roca
Dani Roca

Reputation: 82

create function in line with () => func

I want to create an easy function as a parameter in unity, something like this (in c#):

Invoke((() => { alarm = true; }), 3 );

but this is not working in unity, it says: you cannot convert lambda expressions into strings because invoke needs a string. Any solution?

Upvotes: 2

Views: 120

Answers (1)

Dave Cousineau
Dave Cousineau

Reputation: 13158

Reading about it, Invoke takes a string and float. It probably uses reflection underneath, so it wants a string name of a method to call later. This would mean that you cannot pass a delegate or lambda, but have to use a named method:

Invoke("SetAlarm", 3);

...

void SetAlarm() {
   alarm = true;
}

See:

Upvotes: 1

Related Questions