• 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 Attapi0 · Dec 02, 2013 at 03:47 AM · expected

Odd parenthesis expectation

So I have a script

     #pragma strict
      
      function Update(){
   
      var enemyStats : Stats = gameObject.GetComponent(Stats);
      
      if(Input.GetKey("space")){
          function OnGUI();{
          GUI.Box();{Rect(0,0,10,10); "Your mom, unity";};
          };
      
      
      
      
      };
      }

It's saying on line 8 (The one with the OnGui function) it's expecting a ( instead of OnGui.

Sorry I'm asking 3 thousand questions, I'm just dumb.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by T27M · Dec 03, 2013 at 02:07 AM

Delete line 8 you don't need it. Your OnGui function starting at line 12 should be all caps

 function OnGUI{
 }

It looks as if you are trying to call OnGUI, you don't need to do this it will be called multiple times per frame for you.

 #pragma strict
  
 var enemyStats : Stats = gameObject.GetComponent(Stats);
 var showBox : boolean  = false; 
 
 function Update(){
     if(Input.GetKeyUp("space")){
         showBox = !showBox;
     }
 }
  
 function OnGUI(){
     if(showBox)
         GUI.Box(Rect(20,20,100,100),"This is a title");
 }

I assumed you were trying to show/hide the box when you pressed the space key?

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
1

Answer by AlejandroGorgal · Dec 02, 2013 at 04:04 AM

You have your script backwards, you can't declare a function inside an if statement, the correct format should be:

 #pragma strict
 
 var enemyStats : Stats = gameObject.GetComponent(Stats);
 
 function Update(){
 
 if(Input.GetKey("space")){
 OnGUI();
 }
 }
 
 function OnGui(){
 GUI.Box(Rect(0,0,10,10),"This is a title");
 }

1-The variables should be declared outside of functions unless you want them to exist only when that function is being called (which for the most part you wont want to).

2-You have to end each statement with a Semicolon, but you dont (and shouldn't) add them after a curly bracket.

3-You cant declare a function inside another function. If you want to specify what your OnGui function will do declare separately from the Update function. Look at my script, I have two separate functions, Update and OnGui, inside Update Im saying that if you press the key assigned to the "space" input then it will run the OnGui function (no need to specify it's a function here).

4-Check information on setting up custom input buttons, my guess is that you are trying to use the spacebar to test this script, but unless you go to the input manager, create a new input called "space" and assign a key to it it wont work, if you want to use a specific key the use:

 Input.GetKeyDown(KeyCode.Space)
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 Attapi0 · Dec 03, 2013 at 01:33 AM 0
Share

Jesus, you're concise. Thanks!

Also, to clarify, I'm dumb and couldn't get the variables to work outside of the function, and they don't need to exist outside of them anyways.

avatar image Attapi0 · Dec 03, 2013 at 01:44 AM 0
Share

I think I spoke too soon. I'm getting an error that says "$$anonymous$$ identifier "OnGUI"

To clarify, the error's on line 8.

avatar image rutter · Dec 03, 2013 at 02:17 AM 1
Share

I see two problems with that code.

First, in a case-sensitive language, OnGui and OnGUI are totally unrelated identifiers, not even distant cousins of each other. It's not intuitive, but that's a thing.

Second, calling OnGUI() from your own code is technically allowed but very likely to cause errors at runtime. It's usually better to let Unity call OnGUI, and pass data back and forth with shared variables. Closer to the way @T27$$anonymous$$ does it, in their 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

19 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

Related Questions

Android Unity crashes on splash screen. Invalid serialized file version, expected 3.5.2f2. Actual version: 3.5.3f3 1 Answer

UCE0001 semicolon expected, please help 1 Answer

What am I missing? I already have the semicolon at the end.. 1 Answer

";" expected. Insert a semicolon at the end 1 Answer

Unity Keeps Giving Me Errors On My Scripts 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges