Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ina · Nov 07, 2012 at 08:39 AM · javascriptfunctions

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image valyard · Nov 10, 2012 at 09:41 PM 1
Share

It is a good practice to accept answers or at least upvote them.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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
 }
Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ina · Nov 07, 2012 at 09:26 PM 0
Share

are closures only supported in unity4 then

avatar image ina · Nov 07, 2012 at 09:33 PM 0
Share

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

avatar image valyard · Nov 07, 2012 at 10:40 PM 0
Share

Just tested in 3.5. Works fine.

avatar image valyard · Nov 07, 2012 at 10:41 PM 0
Share

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.

avatar image ina · Nov 07, 2012 at 11:12 PM 0
Share

so you have to wrap it inside a function and cannot directly call it in Start ?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

assigned function declaration in unity javascript for different types 1 Answer

How to pass function as parameter to function? 1 Answer

Does the || operator always work in UnityJS retrieval? 1 Answer

Referencing subclass in Unity JS 2 Answers

How to delete new'd arrays? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges