UnityScript Callback Functions: Cannot cast from source type to destination type.

Is it possible to pass functions as parameters to other script functions in UnityScript? Please note I am not using C# at all.

I’m getting this error:

InvalidCastException: Cannot cast from source type to destination type.
ActorController.FindPathToObject (UnityEngine.GameObject obj, ICallable callback)

When trying to run the code:

_actorCtrl.FindPathToObject(currentTarget, OnPathComplete);

The definition for FindPathToObject:

function FindPathToObject(obj : GameObject, callback: Function){
    // do stuff here
}

The error comes from attempting to pass the callback argument, the gameobject parameter works fine by itself.

Any help would be greatly appreciated

It would usually be better to use the function type, rather than Function, since you define the exact types a function can use.

function Start () {
	var x = Foo;
	Bar(x);
}

function Foo (i : int) : String {
	return (i*2).ToString();
}

function Bar (f : function(int):String) {
	var s : String = f(2);
	Debug.Log (s);
}

In this example, the Bar function can only use functions that take an int and return a string. This is basically the same as delegates in C# (and in fact the function type in Unityscript is compatible with delegates in C#).

–Eric