- Home /
anonymous function with arguments in unityscript
How do you do anonymous functions with arguments in unityscript.
I'm trying to translate this from CS.
webViewObject.Init((msg)=>{Debug.Log(string.Format("CallFromJS[{0}]", msg));});
I've tried this
webViewObject.Init=function(msg:String){Debug.Log("CallFromJS["+msg+"]");};
I get Expression 'self.webViewObject.Init' cannot be assigned to.
Answer by Flipbookee · Sep 27, 2012 at 05:52 AM
Did you try this? webViewObject.Init( function(msg:String) {Debug.Log("CallFromJS["+msg+"]");} );
Answer by Eric5h5 · Sep 27, 2012 at 01:51 AM
I guess the type of webViewObject.Init is wrong. This works:
var foo = function(msg:String){Debug.Log("CallFromJS["+msg+"]");};
foo("Yo");
Or if you want to explicitly declare the type:
var foo : function(String) = function(msg:String){Debug.Log("CallFromJS["+msg+"]");};
You are right. the type is wrong, but I'm not sure how to remedy it. Also not sure how to format text in a comment...it's co$$anonymous$$g out wrong.
Here's some relevant info from the webviewObject class. public void Init(Callback cb null) Callback is declared as System.Action openbracket string closebracket; I tried typing some code in but the commenting system is changing it.
Also not sure how to format text in a comment
Put 4 spaces in front of each line. Or you can format code with the code button in an answer, then copy/paste it into the comment.
Your answer
![](https://koobas.hobune.stream/wayback/20220613081532im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
yield behaves oddly with anonymous functions 1 Answer
Gyroscope to move object left and right 1 Answer
anonymous function in unityscript? 1 Answer
How to simulate a non-english Keyboard 1 Answer
Synchronise translation with Physics 1 Answer