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 markzareal · Aug 14, 2014 at 09:54 AM · javascripterrorfunction

Nested Functions

Firstly, heres my code:

 var target : StartSprite;
 var targetScript : ChangeSprite;
 var AcceptInput : boolean = true;
 private static var score : int = 0;
 var guiScore : GUIText;
 
 function Awake () {
 
     function OnMouseDown () {
         if(!AcceptInput)
         {
             return;
         }
         AcceptInput = false;
         
         if(target.spriteRenderer.sprite == target.diamond) {
             score +=1;
             guiScore.text = "Score: " + score;
             yield WaitForSeconds (0.5);
             targetScript.enabled = true;
         }
         else {
             yield WaitForSeconds (0.5);
             targetScript.enabled = true;
             Debug.Log("Wrong Answer");
         }
     }
     function OnMouseUp () {
         AcceptInput = true;
         targetscript.enabled = false;
         changeSprite ();
     }
 }
 }
 
 function changeSprite (){
     function OnMouseDown () {
         if(!AcceptInput)
         {
             return;
         }
         AcceptInput = false;
         if(target.spriteRenderer.sprite == target.diamond) {
             score +=1;
             guiScore.text = "Score: " + score;
             yield WaitForSeconds (0.5);
             targetScript.enabled = true;
         }
         else {
             yield WaitForSeconds (0.5);
             targetScript.enabled = true;
             Debug.Log("Wrong Answer");
         }
         function OnMouseUp () {
             AcceptInput = true;
             targetscript.enabled = false;
             changeSprite ();
         }
     }
 }
 

Theres a compiling error with nested functions. I searched this up on the internet. I heard that you can't use nested functions in unityscript. I'm a newbie to this. Does anyone have anyway to make the same effect? I really don't know what to do. What's an alternative to nested functions?

Comment
Add comment · Show 3
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 gjf · Aug 14, 2014 at 10:00 AM 0
Share

functions should not be defined within other functions. what are you trying to achieve?

avatar image markzareal · Aug 14, 2014 at 10:11 AM 0
Share

Get the same effect with unity script. I'm asking what is the alternative with nested functions @gjf

avatar image gjf · Aug 14, 2014 at 10:42 AM 0
Share

the formatting is less than ideal, but the code that you've posted looks like you have pasted the same On$$anonymous$$ouseDown() and On$$anonymous$$ouseUp() code into Awake() and changeSprite() and i'm struggling to understand why.

unity will automatically call some functions if you've defined them, such as On$$anonymous$$ouseDown(), On$$anonymous$$ouseUp(), Awake(), etc.

what are you trying to do that makes you think that nested functions are the solution?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by MrSteve1 · Aug 14, 2014 at 12:38 PM

Yeah, function cannot be called inside other function, so you will either need to take them out or for example where your call

 function OnMouseUp()
 {
    blah blah blah
 }


will need to ba changed to

 if(Input.GetMouseButtonUp((0)
 {
      blah blah blah
 }

Hope this helps!

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 markzareal · Aug 14, 2014 at 09:59 PM 0
Share

Yeah!! I never thought of that! One problem, I tried to add Input.GetTouch(0)) but it doesn't work. It gives an error saying something can't be used as a boolean

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

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Function being called when statement untrue? 1 Answer

Script error help! 1 Answer

Robots Won't Spawn With Script 2 Answers

Selecting Object From Top Causes NullReference 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