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 Tdkr80 · Sep 02, 2014 at 01:52 PM · guibuttonparsingunexpectedsymbol

GUI Button not working (Unexpected Symbol)

Here's my script, for some reason it says there's an expected symbol & parsing error.

But if I take away the GUI Button line on line twelve it works fine.

I just want a GUI Texture to show up when I click on a mesh

Here's my script: using UnityEngine; using System.Collections;

 public class OnClick : MonoBehaviour {
     public Texture2D texture = null;
     public float x = 0;
     public float y = 0;
     public float x2 = 0;
     public float y2 = 0;
     void OnMouseDown (){
         if (Input.GetKey ("mouse 0")) {
         GUI.Button(new Rect(x,y, x2,y2), texture)
         }
     }
 }
Comment
Add comment · Show 2
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 Tdkr80 · Sep 02, 2014 at 01:58 PM 0
Share

Now there's no errors, but when I click on the mesh nothing shows up after assigning a texture :/

avatar image Tdkr80 · Sep 02, 2014 at 08:25 PM 0
Share

Now there's no errors, but when I click on the mesh nothing shows up after assigning a texture :/

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by PvTGreg · Sep 02, 2014 at 02:01 PM

you need to put the Gui inside the OnGUI function

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 Tdkr80 · Sep 02, 2014 at 02:03 PM 0
Share

How do I do this? :/ sorry I'm really new to Unity.

avatar image PvTGreg · Sep 02, 2014 at 02:07 PM 0
Share

Hi ok try this at the moment put this outside of the onmouse down for future all gui has to be done inside the OnGUI function

  void OnGUI ()
     {
     GUI.Button(new Rect(x,y, x2,y2), texture)
     }

avatar image PvTGreg · Sep 02, 2014 at 02:07 PM 0
Share

i dont realy know what your trying to do . are you trying to change the texture of the button when you press it?

avatar image
0

Answer by kacyesp · Sep 02, 2014 at 02:06 PM

 public class OnClick : MonoBehaviour {
     public Texture2D texture = null;
     public float x = 0;
     public float y = 0;
     public float x2 = 0;
     public float y2 = 0;
 
     void OnGUI() {
         if ( ! texture ) {
             Debug.LogError("Please assign a texture on the inspector");
             return;
         }

         if ( GUI.Button(new Rect(x,y, x2,y2), texture) )
             Debug.Log( "Do Something" );
     }
 }

Here's a link to the documentation for GUI.Button if you need it: http://docs.unity3d.com/ScriptReference/GUI.Button.html

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 aldonaletto · Sep 02, 2014 at 02:06 PM

GUI items only can appear inside the OnGUI function (or some function called by OnGUI). You could in this case set a variable that enables the GUI item - which should be a GUI.Label or GUI.DrawTexture instead of GUI.Button (a button responds to the mouse click, what may cause weird collateral effects). For instance:

 ...
 private bool enableTexture;

 void OnMouseDown (){
     enableTexture = true; // enable GUI when mouse pressed over object
 }

 void OnMouseUp (){
     enableTexture = false; // disable GUI when mouse button released
 }

 void OnGUI (){
     if (enableTexture){
         GUI.Label(new Rect(x,y, x2,y2), texture);
     }
 }

And remember to end the instructions with a semicolon, like @AyAMrau pointed out.

Comment
Add comment · Show 2 · 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 Tdkr80 · Sep 02, 2014 at 02:12 PM 0
Share

Thank you so much, i've been for the past hour for a script like this.

This works perfectly, and now that I know how to set variables in C# like that, there's no stopping me!

avatar image Tdkr80 · Sep 02, 2014 at 02:13 PM 0
Share

also, how am I supposed to add buttons to click on when i click on a mesh then, if I can't add a GUI Button?

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

Parsing Error : Unexpected symbol 'end of file' 2 Answers

changing GUI Button text with a string array 2 Answers

Problem with multi-touch on android game 1 Answer

Add EditorUI elements when a button is pressed 0 Answers

Bullet Prefab not instantiating 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