• 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
Question by oliver-jones · Nov 21, 2010 at 01:38 AM · mousemouseclickclickable

How To Perform A Mouse Click On Game Object

Hello,

I would like the ability to click on an object in the game play.

Eg: user clicks on cube in the game, a dialog window then pops up, or an event is triggered.

I have no person controls or anything, just a camera.

--- Snippet of Scrrpt -----

function Update () 
{
    if(Input.GetMouseButtonDown (0) && Clicked == false)
    {

This works, but I can click anywhere on the screen....

Comment
Socapex
MountDoomTeam
Gaming-Dudester
FingerlessEntertainment

People who like this

4 Show 0
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

5 Replies

  • Sort: 
avatar image
Best Answer

Answer by Uriel_96 · Nov 21, 2010 at 02:12 AM

try this

if ( Input.GetMouseButtonDown(0)){
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var select = GameObject.FindWithTag("select").transform;
if (Physics.Raycast (ray, hit, 100.0)){
select.tag = "none";
hit.collider.transform.tag = "select";
}
}

Comment
oliver-jones
Socapex
Kmulla
SrBilyon
Gaming-Dudester
zakirshikhli
Bob-The-Zealot
FingerlessEntertainment

People who like this

8 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 oliver-jones · Nov 21, 2010 at 02:40 AM 0
Share

Okay, but what about when I want to deselect it or unclick it ... Thanks

avatar image Uriel_96 · Nov 21, 2010 at 03:33 AM 0
Share

good point, Im not completly sure but I will try to put an else something like this maybe: if (Physics.Raycast (ray, hit, 100.0)){}else{//Do what you want}

avatar image TheJables · Aug 13, 2012 at 04:10 PM 0
Share

I've been looking for a way to detect if you've clicked on a game object and if so, destroy it. However, I'm getting some errors just trying to implicitly declare var hit and var ray to the different RaycastHit and Ray structs. I assume that there's no additional using statements needed here? Is there something I might be missing? I've copied the code above as is but it doesn't seem to like the ':' after hit and ray.

avatar image kamal2013thapa · Apr 06, 2013 at 12:19 PM 0
Share

I am also facing same problem.When I mouse over on the object it move.But i want to move, when left mouse clicked.

kamal thapa april 6th, 2013

avatar image

Answer by JakeElliott · Nov 21, 2010 at 02:21 AM

You can use the OnMouseDown function in a script component attached to your cube, as long as it has a collider: http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnMouseDown.html

Comment
Lance
madmike6537
Novodantis
lalamax3d
Bryan-Legend
Lo0NuhtiK
Free Arm Studios
grigdog
oleh.kolinko
Fewpwew130
ruimelo
WickedFreaK
mannyhams

People who like this

13 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 Screenhog · Aug 13, 2012 at 04:37 PM 1
Share

This is true, but beware that OnMouseDown (and similar functions) don't work on mobile devices. Depending on your needs, this might not be a problem, but I thought it worth mentioning anyway.

avatar image

Answer by jharri30 · Jan 17, 2014 at 03:08 AM

I was having some issues with Uriel's code, so I tweaked it a little to simplify it. Here is the solution that worked for me.

I created a cube which had a "Box Collider" on it by default and renamed it to "targetArea". I then added a script to it with the following update statement...

 function Update ()
 {
     if ( Input.GetMouseButtonDown(0))
     {
         var hit : RaycastHit;
         var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         
         if (Physics.Raycast (ray, hit, 100.0))
         {  
             Destroy(GameObject.Find("targetArea"));
         }
     }
 }
Comment
talatsteelgleam

People who like this

1 Show 3 · 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 TSCGCobra043 · Apr 04, 2016 at 05:12 PM 0
Share

do you know how to do this but instead when clicked will load a new scene?

avatar image derlin TSCGCobra043 · Sep 10, 2016 at 02:10 PM 0
Share

Just replace Destroy(...); with the following sceneloading function.

using UnityEngine.SceneManagement; //add to top of script

SceneManager.LoadScene(string);

avatar image Stevens-R-Miller · Dec 21, 2017 at 11:06 PM 0
Share

Long time later, but I am a bit confused by this code. While it tests to see if the ray hit anything, it doesn't seem to query for what it hit. If you only have one collider, then a hit has to be on that collider. But what if you have two or more? How do you know which one you hit?

avatar image

Answer by OgiJr · Dec 26, 2015 at 11:15 AM

Void OnMouseClick() { function }

Comment
IBuro

People who like this

1 Show 0 · 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

Answer by AlisVitae · Jun 21, 2015 at 06:02 PM

Look at our tutorial! https://www.youtube.com/watch?v=HR_88quPMog

Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

action to click the left mouse 1 Answer

Unity Web Player does not respond to mouse clicks 1 Answer

EventSystem - detect any click NOT on object 2 Answers

Problem with P.C. touchpad. 1 Answer

Make a game object appears at mouse position when clicked 4 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