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 Mattias-Wargren · Oct 17, 2011 at 05:47 AM · mouseselectionselect

Click anywhere to deselect active GameObject

I am building a ObjectSelector script that every selectable GameObject will use. The class communicates with the class ClickHandler which stores the current selected GameObject.

What is the best approach to register clicks that are not on any of the selectable GameObjects? If the player clicks anywhere in the game I would like the ClickHandler to deselect the active 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

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by save · Oct 17, 2011 at 10:26 AM

You could deselect it first then if a physics.raycast hits an object that is selectable you select it. To determine if the object is selectable you could have a script (which could be empty) attached.

if (Input.GetButtonDown("Fire1")) {
    ClickHandler.SelectedObject = null;
    var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    var hit : RaycastHit;
    if (Physics.Raycast (ray, hit, 100)) {
        if (hit.transform.GetComponent(selectable)) {
            ClickHandler.SelectedObject = hit.transform;
        }
    }
}

The code is not tested but should give you an idea of what to do.

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

Answer by mnml_ · Oct 05, 2017 at 02:19 PM

I solved this without a 'catch-all-collider':

https://docs.unity3d.com/Manual/ExecutionOrder.html states that input events are always processed before any update/coroutines. so I have a behaviour containing OnMouseDown and OnMouseUp handlers on my gameobjects with colliders which detect clicks, and one (singletonized) behaviour doing the same 'click-detection-logic' in a coroutine (watching Input.GetMouseButtonDown and Input.GetMouseButtonUp) when a gameobject with collider detects getting clicked it sets a bool-flag on the singleton (bool clickSourceKnown) true. when the singleton detects a click and clickSourceKnown is false it was a 'click in the air' - otherwise it resets the clickSourceKnown to false and continues watching the Input...

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 Evolution · Oct 10, 2017 at 03:22 PM 0
Share

This is brilliant! A super simple solution, yet powerful.

avatar image
0

Answer by swisscoder · Oct 17, 2011 at 06:56 AM

you can draw a big big collider around your scene, where you catch the OnClick too. So you can deselect the last GameObject from the ObjectSelector when you click "anywhere" (anywhere=>click on the big collider around your scene)

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 Mattias-Wargren · Oct 17, 2011 at 10:01 AM 0
Share

I was hoping to avoid the "big collider" idea. Looking for a general click ins$$anonymous$$d of relying on a GameObject.

avatar image swisscoder · Oct 17, 2011 at 10:19 AM 0
Share

you could catch the $$anonymous$$ouseButtonDown in a "global" like method, but then you wouldn't know if it was while clicking a GameObject or not. But with script execution order you could probably check before and after the marked GameObject if a new one was clicked..

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

6 People are following this question.

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

Related Questions

Move selected object. 1 Answer

How to NOT select an object behind a GUI? 1 Answer

Box Selecting Top-Level Transforms Only 0 Answers

How to select a hidden MeshRenderer in SceneView? 0 Answers

How to multi-select scene hierarchy objects in code 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