Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
4
Question by Peter B-B · Jan 29, 2010 at 04:18 AM · gameobjectmouse

Detecting when a game object is clicked, when the mouse is held down, or enters or leaves a GameObject

I stumbled upon this post which was pretty useful:

http://answers.unity3d.com/questions/3209/basic-on-off-movement-using-mouse-click

My dilemma isn't exactly his though. His is more toggling a button while mine is a hold.

Problem 1: The button should de-activate when onMouseUp, easy modification, but it introduces another problem:

Problem 2: The button should de-activate when the mouse leaves its area. So if the guy drags his mouse off the button, it should deactivate.

Anybody know how to program this?

Thanks in advance!

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 j1c2 · Jan 09, 2013 at 05:58 PM 0
Share

thanks it works!!! =)

avatar image Loius · Jan 09, 2013 at 06:06 PM 0
Share

Asked January 29. Of last year (or, probably, earlier). :P

2 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by duck · Jan 29, 2010 at 12:03 PM

There's an OnMouseExit event which you can use just like OnMouseUp, to trigger code when the mouse is no longer over the object. You'd probably have to use this in conjunction with the mouse up/down tests to get the effect you want.

(note, the comments appear to be wrong in the example on the documentation page linked above)

Here's an example which activates an object when clicked, and deactivates when the mouse is released, or when the mouse exits. Notice that it also reactivates if the mouse is brought back over the object while still held down (standard behaviour for normal Mac and Windows UI buttons). I'm using a boolean variable called 'wasClicked' to remember the state, to achieve this. The Activate() and Deactivate() functions are custom functions - I have just added code which makes the object change colour as an example, but you can put whatever you want in there.

var wasClicked : boolean;

function OnMouseDown() { wasClicked = true; Activate(); }

function OnMouseUp() { wasClicked = false; Deactivate(); }

function OnMouseEnter() { if (wasClicked) { Activate(); } }

function OnMouseExit() { Deactivate(); }

function Activate() { renderer.material.color = Color.red; }

function Deactivate() { renderer.material.color = Color.white; }

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 Peter B-B · Jan 29, 2010 at 04:03 PM 1
Share

Thank you so much it works!!

How would I go about doing this for an iPhone seeing as how the iPhone doesn't have the mouse functions (I think)?

avatar image cregox · Dec 03, 2010 at 02:51 PM 0
Share

@Peter I wish that comment had been answered...

avatar image cregox · Dec 03, 2010 at 08:21 PM 0
Share

But since it haven't, this is what I could use for iphone: http://forum.unity3d.com/threads/15377-quot-On$$anonymous$$ouseDown()-quot-or-quot-On$$anonymous$$ouseUp-quot-On-GUIT

avatar image infiniTesimal91 · Nov 11, 2013 at 09:09 PM 0
Share

Do I assign this script to the object we want to whether is clicked or not?

avatar image
0

Answer by Vinícius MM · Sep 23, 2012 at 09:14 PM

Thank you so much! Very useful.

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

C# Gameobject Rigidbody Mouse Collision 1 Answer

trying to move an object with the mouse 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

move Game Object(hand) with mouse movement 2 Answers

Best practice for copying Components from one GameObject to another? 3 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