• 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 joshking518 · Feb 05, 2013 at 08:51 PM · guiraycastonmouseup

Calling OnGUI after clicking an object?

Hey guys I am trying to call the OnGUI function after clicking an in-game object. It is a computer and when you click it I want a gui to pop up with buttons on things you can do. My problem is, when I click the object, the gui never pops up. If I delete the 'if' function, the gui is always there until I click on the object. 'Destroy(Cube)' is just there for debugging purposes.

 var Cube:GameObject;
 var hit : RaycastHit;
 var guiEnable:boolean = false;
 
 function OnMouseUp(){
     if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit)){
         Destroy(Cube);
         guiEnable = true;
         OnGUI();
     }
 }
 
 function OnGUI()
 {
     if(guiEnable){
         GUI.Button(Rect(0,0,200,100), "YEY!!!");
         }
 }
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
0

Answer by Dave-Carlile · Feb 05, 2013 at 09:12 PM

OnGUI is called automatically - you shouldn't be calling it yourself. Setting your guiEnable property to true should be sufficient for the button to appear.

Comment
Add comment · Show 6 · 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 joshking518 · Feb 05, 2013 at 09:27 PM 0
Share

Commenting out the OnGUI call does nothing. The button still never shows up

avatar image Dave-Carlile · Feb 05, 2013 at 09:31 PM 0
Share

Add a Debug.Log inside your raycast if to make sure it's getting in there to set guiEnabled to true. Also, if this script is attached to the cube, then destroying the cube will prevent the GUI from displaying since the script will be destroyed along with it.

avatar image joshking518 · Feb 05, 2013 at 09:41 PM 0
Share

The script WAS attatched to the cube. I attatched it to the player and unattatched it from the cube. Now, the cube isnt destroyed when clicking on it.

avatar image Dave-Carlile · Feb 05, 2013 at 09:45 PM 0
Share

Does the Cube property still reference the cube, after you moved the script? Did you add the debug call? It's for sure setting guiEnabled to true?

avatar image joshking518 · Feb 05, 2013 at 09:46 PM 0
Share

The cube is still being referenced. I added Debug.Log; in the if statement but I got an error "expressions may only be used for their side effects". Thanks for your help btw

Show more comments
avatar image
0

Answer by Benproductions1 · Mar 15, 2013 at 01:34 AM

Hi there,

Input.mousePosition's axis is at the bottom left of the screen, while ScreenPointToRay's is in the top left. To compensate you have to Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y) See if that works.

Another problem is you not checking if your raycast is hitting the cube, only wether it's hitting anything. You also shouldn't name it Cube since that is a variable built into unity as seen by the highlights. Try renaming it to just cube.

Hope this helps, Benproductions1

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

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

How can I add a distance cap to this script? 3 Answers

Disabling Raycast with GUI 0 Answers

Problem with GUI Buttons with WorldSpace Canvas 2 Answers

Is there a generalized version of GUIUtility.hotControl? 0 Answers

How to connect RayCast and GUI Text? 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