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
1
Question by NanjaKilo · Apr 17, 2015 at 07:37 AM · functionscriptingbasicsarchitecturenested

workaround for lack of nested functions in Unity?

I just discovered that nested functions do not work due to Unity's architecture. so is there anyway to achieve essentially the same thing by calling an entire script full of functions within the function in another script? what would that code like in JS? Thanks in advance.

Comment
Add comment · Show 9
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 AlwaysSunny · Apr 17, 2015 at 07:15 AM 1
Share

Tell us what a nested function is?

 void A() {}
 void B() {}
 
 void Both() {
   A();
   B();
 }
avatar image Bonfire-Boy · Apr 17, 2015 at 08:38 AM 1
Share

"is there anyway to achieve essentially the same thing...?"

We can't answer that without knowing what it is you're trying to achieve. The way nested functions work can vary between languages but the main uses that spring to $$anonymous$$d are 1) to restrict access to the nested function and 2) to make enclosing function data available to the nested function. Other than that, you get the same functionality by just not nesting them. If you think it's important to restrict access I guess the "workaround" would be to use namespaces and/or classes to wrap things, for the other thing you could just pass parameters in.

I don't see the lack of nested functions as a Unity thing in particular. They're not usually a feature of C-style languages.

avatar image Owen-Reynolds · Apr 17, 2015 at 03:43 PM 1
Share

I think most people just use a Class, where other member functions act as the "nested" functions (and a Unity script is a class, so, I guess the same thing you said.)

I think the idea is, once you have classes, you no longer need nested functions.

avatar image Glurth · Apr 17, 2015 at 04:26 PM 0
Share

I guess I'm being semantic but I thought a nested function was when you define a function, inside another function.

 Outside()
 {
   float outsideLocalVariable;
   ..stuff..
   Inside(int a){  ..do stuff using outsideLocalVariable..  }//declare nested function
   ..stuff..
   Inside(1);  //call the nested function
   if(..some condition...)
       Inside(2);  //call the nested function
 } 

This DOES work in unity (c#, at least). The REASON I sometimes use this is because all the local variables defined in Outside(), are within scope to the Inside() function: this can save a lot of parameter passing. (as bonfireboy's #2 above mentions)

EDIT: this comment is wrong! see correcting comment below.

avatar image Bonfire-Boy · Apr 17, 2015 at 04:47 PM 0
Share

@Glurth That's the usual meaning of "nested function" and the interpretation I was assu$$anonymous$$g. I get compiler errors (in Unity and $$anonymous$$onoDevelop) when I try that.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by cdrandin · Apr 17, 2015 at 04:08 PM

if they have similar parameter signature then you could go about using delegate or anonymous methods.

 void A()
 {
     Debug.Log("A");
 }
 
 void B()
 {
     Debug.Log("B");
 }
 
 delegate void DelegateController();
 
 DelegateController d = ()=>{A (); B ();};
 d.Invoke(); // <- this is what executes both functions

Output:

 A
 B


Resources: Delegates Actions Func

Comment
Add comment · 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
0

Answer by NanjaKilo · Apr 17, 2015 at 10:27 PM

So here are the various functions that I want to reduce to one "MechName" function called Shadowcat. that way when I click something to invoke the Shadowcat function in a GUI setting of the loadout screen, all the data for that Mech is called forth from Voodoo Child.

 function ShadowcatCenterTorso ()
 {
     BackstageTonnage.Hardpoint = 1;
     BackstageAmmo.Laser = true;
     BackstageArmor.Armor = 30;
 }
 
 function ShadowcatRightTorso ()
 {
     BackstageTonnage.Hardpoint = 2;
     BackstageAmmo.Missile = true;
     BackstageArmor.Armor = 20;
 }
 
 function ShadowcatLeftTorso ()
 {
     BackstageTonnage.Hardpoint = 2;
     BackstageAmmo.Missile = true;
     BackstageArmor.Armor = 20;
 }
 
 function ShadowcatRightArm ()
 {
     BackstageTonnage.Hardpoint = 2;
     BackstageAmmo.Laser = true;
     BackstageArmor.Armor = 10;
 }
 
 function ShadowcatLeftArm ()
 {
     BackstageTonnage.Hardpoint = 2;
     BackstageAmmo.Ballistic = true;
     BackstageArmor.Armor = 10;
 }
 
 function ShadowcatRightLeg ()
 {
     BackstageArmor.Armor = 10;
 }
 
 function ShadowcatLeftLeg ()
 {
     BackstageArmor.Armor = 10;
 }
Comment
Add comment · Show 1 · 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 Bonfire-Boy · Apr 20, 2015 at 12:03 PM 0
Share

You need to add this to your question, rather than post it as an answer.

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

6 People are following this question.

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

Related Questions

Script architecture for tile-based tower-defense game 1 Answer

Unityscript: Accessing functions from other scripts 1 Answer

running code outside of update! 3 Answers

Is it ok to use nested classes, to cache and access components? 1 Answer

Best way to make these tree iterations? Arrays? tags? nested etc 1 Answer


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