• 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 Charsmud · Jul 30, 2012 at 02:52 PM · guiclickonmousedownonmouseup

Text popup onMouseDown

Hello! I currently have a script that activates when the player's mouse hovers over the hitbox of whatever the script is attached to. However, I cannot for the life of me figure out how to use it with onMouseDown and onMouseUp. I have this script:

 var _mouseOver = false;
 
 function OnGUI()
 {
     if(!_mouseOver) {
     return;
     }
  GUI.Label (Rect (10, 10, 100, 20), "Clicked a key!");
 }
 function OnMouseOver()
 {
     _mouseOver = true;
 }
 function OnMouseExit()
 {
     _mouseOver = false;
 }

I have tried changing OnMouseOver and OnMouseExit, but to no avail. How would I do this?

I think that this is caused by my key script:

 #pragma strict
   
       var collected = false;
 
     function OnMouseDown() {
          collected = true;
          Destroy(gameObject);
     }


 
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
Best Answer

Answer by termway · Jul 30, 2012 at 03:24 PM

I'm not sure, I have understood all your need. But if you want to detect when a mouse is over and click down on an object, this script is for you :

 var _mouseOver = false;
 
 function OnGUI(){
     if(_mouseOver) {
          GUI.Label (Rect (10, 10, 100, 20), "Clicked a key!");
     }
  
 }
 function OnMouseOver() {
     _mouseOver = true;
 }
 function OnMouseExit() {
     _mouseOver = false;
 } 
 function OnMouseDown () {
    if(_mouseOver){
        Debug.Log("Over && Down");
    }
 }


Update:

Something like this ?

 var _mouseDown = false;       
 function OnGUI() {
     if(_mouseDown) {
         GUI.Label (Rect (10, 10, 100, 20), "Clicked a key!");
     }
 }  
 function OnMouseDown () {
     _mouseDown = true;
 }  
 function OnMouseUp () {
     _mouseDown = false;
 }
Comment
Add comment · Show 4 · 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 Charsmud · Jul 30, 2012 at 03:26 PM 0
Share

I want ins$$anonymous$$d of hovering to activate gui, you click.

avatar image Charsmud · Jul 30, 2012 at 03:43 PM 0
Share

Hm... the text is not appearing... is it because i destroy the key after I click it?

avatar image termway · Jul 30, 2012 at 03:55 PM 0
Share

Yes it delete your object and your script. "Actual object destruction is always delayed until after the current Update loop, but will always be done before rendering." http://docs.unity3d.com/Documentation/ScriptReference/Object.Destroy.html

avatar image Charsmud · Jul 30, 2012 at 03:57 PM 0
Share

thanks, got it workin! :)

avatar image
0

Answer by TwistedGalaxy · Jul 30, 2012 at 04:01 PM

I am thinkng that this is what you are looking for:

     var _mouseOver = false;
     var _mouseClick = false;
 
 function OnGUI(){
     if(_mouseClick) {
          GUI.Label (Rect (10, 10, 100, 20), "Clicked a key!");
     }
 
 }
 function OnMouseOver() {
     _mouseOver = true;
 }
 function OnMouseExit() {
     _mouseOver = false;
 } 
 function OnMouseDown () {
    if(_mouseOver){
        _mouseClick=true;
    }
 } 
 function OnMouseUp () {
        _mouseClick=false;
 }

The script makes the text show when you press your mouse button and while hovering over the location. Then when you release the mouse button, the text goes away. If you want the text to stay after mouse click, simply remove the OnMouseUp function.

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 Eowyn27 · Dec 10, 2013 at 03:57 PM 0
Share

How do I use this script with my object? Do I just create a new class called Textlabel.js and attach this piece to my object?

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

OnMouseDown 2 Answers

How to: Click top Object of 2 overlapping objects (2D) 1 Answer

Display message when OnMouseDown 2 Answers

GUI and Application do no exist in the context 2 Answers

Can't click gameobject when over another trigger? 1 Answer

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