- Home /
Closure does not work in Unity JS functions?
var complicated = function (init:float){ init = init * 2; var subfunction = function(passme:float){ var newval = init*passme; }; };
Is it possible to have a "subfunction" access the variables and parameters of the parent function?
Internal compiler error. See the console log for more information. output was:BCE0011: An error occurred during the execution of the step 'Boo.Lang.Compiler.Steps.EmitAssembly': 'Argument cannot be null.
It is a good practice to accept answers or at least upvote them.
Answer by valyard · Nov 07, 2012 at 02:05 PM
The error must be somewhere else. In Unity 4 this code works fine:
#pragma strict
function Start () {
bla();
}
function bla() {
var complicated = function (init:float){
init = init * 2;
var subfunction = function(passme:float){
init = init*passme;
return init;
};
return subfunction;
};
var c = complicated(3);
Debug.Log(c(5)); // 30
Debug.Log(c(2)); // 60
Debug.Log(c(3)); // 180
}
... actually it seems adding the return subfunction works - also, it appears declaring var inside of subfunction was what yielded the error... is it possible to declare variables inside of subfunctions in unity 4?
Did you try to write this code in an empty JS file? You should use function complicated(init:float){ syntax ins$$anonymous$$d in this case.
so you have to wrap it inside a function and cannot directly call it in Start ?