- Home /
passing a function as a parameter to another function
is it possible to do something along the lines of this (in Javascript)
function foo() {
print("hello Unity");
}
function boo(passedFunction) {
yield new WaitForSeconds(2) //wait then
//call passedFunction
}
boo(foo);
thanks in advance,
Chris
Answer by smokris · Nov 25, 2009 at 12:02 AM
Yes, you can do that using the following syntax:
// define your inner function as a named variable var foo = function() { print("hello Unity"); }
// define your outer function normally function boo(passedFunction) { // call the passed function passedFunction(); }
// call the outer function using the named-variable-function boo(foo);
This is funny, because Unity doesn't allow anonymous functions before version 3.0. Your post though is from back in 2009. So - How the hell did you get that to work?!
.. and when using #pragma strict (eg on Android or iPhone), the data type of a function is Function (eg, use "function boo(passedFunction : Function) {". ( http://answers.unity3d.com/questions/9594/data-type-of-unityscript-function-object.html )
Your answer
Follow this Question
Related Questions
Javascript / Unityscript 'with' keyword 1 Answer
If statment based on a function not working 3 Answers
Passing an array to a function by reference? 1 Answer
Calling function in a script based on raycast hit 1 Answer
accessing a function? 1 Answer