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 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.

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

Display message when OnMouseDown 2 Answers

OnMouseDown 2 Answers

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

Player Fast Travels 1 Answer

How to disable a part of a script to avoid replayed camera animations ? 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