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 /
This question was closed Jun 19, 2014 at 05:53 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Ssiroo · Jun 19, 2014 at 03:24 PM · javascriptfunctiononguiontriggerenter2d

BCE0044: expecting (, found 'OnGUI'.

Hello,Im trying to make a "game over,restart" menu popup when a 2D collider enters a trigger.At first I made a scene where it shows you a 'game over' text and you had to press the retry button to restart and it worked great.Now when I try to make some buttons show up,it gives me these errors :

"Assets/Scripts/Destroyer.js(15,10): BCE0044: expecting (, found 'OnGUI'."

And this one :

"Assets/Scripts/Destroyer.js(15,17): UCE0001: ';' expected. Insert a semicolon at the end."

This is the script im using :

 #pragma strict


 public var other : GameObject;
 var myGUISkin : GUISkin;

 function OnTriggerEnter2D(other : Collider2D)
 {
 if(other.tag == "blackballoon")
 {
 Destroy (other.gameObject);
 }
 if(other.gameObject.tag == "balloon")
 {
 function OnGUI(){
 GUI.skin = myGUISkin;

 if (GUI.Button(Rect(Screen.width/2-100,Screen.height/2+3,200,50),"Restart")){
 Application.LoadLevel(Application.loadedLevel);
     }
 }
 }
 }


Thnak you for your time.

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

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Jun 19, 2014 at 03:26 PM

You need to take the time to indent and match your braces. Pick a single bracketing style, not the mix and match as you've done here. Once you have your bracketing fixed, you will see that your OnGUI() function is nested inside your OnTriggerEnter2D function. You cannot nest functions. Pulling OnGUI() out to the top level will fix this error.

Comment
Add comment · Show 3 · 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 Ssiroo · Jun 19, 2014 at 03:44 PM 0
Share

What exactly do you mean by pulling OnGUI() on top,if I do then wouldn't the function be called before the object enters the 2d trigger ?

avatar image robertbu · Jun 19, 2014 at 04:24 PM 0
Share

If you want to prevent GUI object from showing until the object enters the trigger, you must use a boolean flag.

 #pragma strict
  
 public var other : GameObject;
 var myGUISkin : GUISkin;
 var showGUI = false;
  
 function OnTriggerEnter2D(other : Collider2D) {
     if(other.tag == "blackballoon") {
         Destroy (other.gameObject);
     }
     if(other.gameObject.tag == "balloon") {
         showGUI = true;
     }
 }
 
 function OnGUI(){
     if (showGUI) {
         GUI.skin = myGUISkin;
          
         if (GUI.Button(Rect(Screen.width/2-100,Screen.height/2+3,200,50),"Restart")){
             Application.LoadLevel(Application.loadedLevel);
         }
     }
 }

Again, you cannot nest functions. OnGUI() is a callback that gets called every frame, so you cannot call OnGUI() from inside OnTriggerEnter2D() either.

avatar image Ssiroo · Jun 19, 2014 at 04:37 PM 0
Share

I get it now,thank you.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Performance Optimization ~Function Update: Loop or Once ? 5 Answers

Disable function OnGUI after 4 seconds 1 Answer

GUI opacity. 1 Answer

Functions overwrite or work seperatelly ? 1 Answer

javascript equivalent of Action? 0 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