• 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 DarkSlash · Oct 15, 2015 at 10:47 PM · onmousedown

Contrary to "OnMouseDown()"?

I have a gameobject that activates its child when its clicked on (I get the click through OnMouseDown() method). I want that the same gameobject disables its child when the player clicks somewhere else (not over it). Is there any contrary function to OnMouseDown() to get that click? Or I have to use raycast to see if the player clicked on it or not? I'm usingUnity 5

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 SpaceFace15 · Oct 16, 2015 at 01:13 AM

Yes, there are a lot more mouse functions, but I just don't know them off the top of my head or however that saying goes. I'm not a Professional at coding but you could do something like

 void OnMouseDown()
       {
          //enable your object
       }
     void OnMouseExit()
     {
      //which will happen whenever someone closes
      if(input.GetButtonDown("Fire 1")
     
        {
                   //Anywhere that is not the object.
     
         }
      }

Hope I helped I'm still learning. But I'll put a link right here:http://docs.unity3d.com/ScriptReference/MonoBehaviour.html

All of the functions in Monobehavior which is the source for all functions in unity.

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 DarkSlash · Oct 16, 2015 at 12:00 PM 0
Share

On$$anonymous$$ouseExit just fire when the mouse leaves the over position of the object, so you have to exit the object and click the button at the same time for that code to be executed. Something with mouseenter mouseexit and a bool can be done, as @DiegoSLTS suggests.

avatar image
0

Answer by DiegoSLTS · Oct 16, 2015 at 04:00 AM

I guess a function like that would create a lot of overhead, calling an "OnMouseDownNotOverThisObject" on EVERY object (that's not the one clicked) is a bit too much considering the click is a pretty common input method.

You probably need a "workaround", like:

  1. The raycast you mentioned. Or...

  2. Use OnMouseEnter so set a bool to true, OnMouseExit to set it to false, check for mouse clicks on Update and instead of the raycast use the bool variable (if true, you clicked on the object).

I'm not sure if those can be called workarounds since I can see why something like that is not implemented as an event that's send to every object.

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 DarkSlash · Oct 16, 2015 at 12:01 PM 0
Share

Yes I'm about to use the (2) solution. I don't understand your last paragraph, can you explain it?

avatar image DiegoSLTS DarkSlash · Oct 16, 2015 at 12:57 PM 0
Share

It's not really important, I just ment that I usually see the word "workaround" used as an alternative to something that can't be done, but in this case it's just an implementation of a function. It's like making a function that fades a sprite and saying it's a workaround for not having a function already implemented that does that. A workaround would be something like adding an invisible game object behind all clickable object, and when clicking that object calling a "NotClicked" function in the objects you want. I usually do this to hide UI popups, to detect a click outside the popup I just place a panel that matches the screen size, and a click in that panel means "a click outside the popup".

And by "I can see why it's not implemented as an event" I just wanted to resfresh what I wrote in the first paragraph, calling a function on every object every time there's a click looks a bit overkill for something that's not that common and can be easily done on the objects that needs that. I've read that Unity calles this standard monobehaviour functions doing a Send$$anonymous$$essage, if it has to do that for every object on every click it could hurt performance, even if no object implement a function for that. There are functions like OnApplicationPause that do that, but they're called in moments where a hit in performance could be acceptable.

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

30 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

OnMouseDown with 360 controller unity? 1 Answer

GameObject Array 1 Answer

Increase mouseclick area size for OnMouseDown() - For Mouse Shooting Game (Open to more ideas) 2 Answers

Click on a gameObject to show/hide a GUI box help C# 1 Answer

Collider priority with onMouseDown() 2 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