- Home /
UnityScript equivalent to C# Action
Hi, all! I recently had an extremely lengthy discussion on Finite State Machines and was given much help from the community. I have reached a higher level of understanding and I know what needs to be done now, but I cannot figure out how to do it.
In my earlier thread (found here), whydoidoit has a tutorial and a reflection/delegate approach to writing a Finite State Machine. It looks very neat and tidy and the code is easily editable and more efficient in this form.
To make a reference to a function in his example script, he declares the delegate of tye Action.
var whatToDoOnUpdate : Action;
function FooBar(){
print("running FooBar");
}
function Start(){
whatToDoOnUpdate = FooBar();
}
function Update(){
whatToDoOnUpdate();
}
Why can I not do this in UnityScript? I get errors thrown at me. Is there an equivalent to this?
Thanks!- YA
Oh! Sorry. Forgot about that ;)
First I get this:
The name 'Action' does not denote a valid type ('not found'). Did you mean 'System.Action'?
When I put in System.Action, I get this:
Cannot convert 'void' to 'function(): void'.
What should I do?
-Thanks! - YA
Answer by Bunny83 · Nov 28, 2012 at 11:39 PM
As you might know this:
FooBar();
will invoke the function FooBar and return it's return type which is void. You try to assign this "return value" to your Action. You have to assign the function itself. I've never tried that in Untiyscript, but if it works then it should look like this:
whatToDoOnUpdate = FooBar;
Oh. $$anonymous$$y. Gosh. I can't believe I made such a simple mistake! Thank you very much! I still had to use System.Action but otherwise it worked like a charm. Thank you so much! - YA
Your answer
![](https://koobas.hobune.stream/wayback/20220613084649im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Should I use c# or Unityscript? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
javascript equivalent of Action? 0 Answers