x


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

more ▼

asked Nov 24 '09 at 11:40 PM

chris Etches gravatar image

chris Etches
118 4 5 7

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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);
more ▼

answered Nov 25 '09 at 12:02 AM

smokris gravatar image

smokris
366 5 6 14

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?!

Nov 29 '10 at 06:19 AM Whimsical

.. 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 )

Apr 29 '12 at 08:20 AM pansapiens
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x498
x110
x2
x1

asked: Nov 24 '09 at 11:40 PM

Seen: 4750 times

Last Updated: Apr 29 '12 at 11:19 AM