Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
10
Question by MH1 · Oct 14, 2012 at 02:13 AM · c#click

How do you make an object respond to a click in C#

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

4 Replies

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

Answer by fcoarmstrong · Mar 02, 2013 at 04:50 PM

place void OnMouseDown () { ....} outside of the update method like this:

 void Update () {
     
 }
  void OnMouseDown(){
         // this object was clicked - do something
     Destroy (this.gameObject);
  }   
Comment
Add comment · 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 gudus · Sep 18, 2014 at 07:03 AM 0
Share

For using this method ("void On$$anonymous$$ouseDown()") object hast have component "Rigidboby"...

avatar image nguyenlamlll gudus · Jan 14, 2018 at 01:20 PM 0
Share

To be clear, Here they say that method is "...called when the user has pressed the mouse button while over the GUIElement or Collider".

avatar image $$anonymous$$ · Dec 30, 2016 at 05:59 PM 0
Share

This Really Helped With $$anonymous$$y Game Development Thanks! ;)

avatar image
43

Answer by aldonaletto · Oct 14, 2012 at 02:36 AM

There are two main alternatives:
1- Use OnMouseDown in the object script. This is the best alternative when you want only some objects to respond to clicks - only the ones that have OnMouseDown in their scripts will sense the click:

 void OnMouseDown(){
   // this object was clicked - do something
 }

2- Use Physics.Raycast to find which object is being clicked - this is better when you want to click any object in scene, since a single script attached to the camera does the job:

 void Update(){
   if (Input.GetMouseButtonDown(0)){ // if left button pressed...
     Ray ray = camera.ScreenPointToRay(Input.mousePosition);
     RaycastHit hit;
     if (Physics.Raycast(ray, out hit)){
       // the object identified by hit.transform was clicked
       // do whatever you want
     }
   }
 }
 

NOTE: Only scene objects that have a collider can be clicked, with exception of GUITexture and GUIText, which respond to OnMouse events (but not to Raycast)

Comment
Add comment · Show 6 · 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 Huminaboz · Apr 04, 2013 at 07:51 AM 0
Share

This helped, thanks!

avatar image Matt.Lebrao · Apr 12, 2016 at 12:56 AM 0
Share

I get it, but where should the second method go? There isn't a universal script we can make, is there?

avatar image MysteryPyg · Apr 21, 2017 at 08:08 PM 0
Share

By the way, to reference the object that was hit, use "hit.collider", not "hit"...

avatar image yayarokya · Mar 07, 2020 at 08:58 AM 0
Share

Hello. Thank you for your answer. Could you, please, clarify one more point? What do you mean by the this is better when you want to click any object in scene? Won`t I be able to register a click on any object in scene if I use the On$$anonymous$$ouseDown?

avatar image nubdev yayarokya · Sep 04, 2020 at 01:59 AM 0
Share

On$$anonymous$$ouseDown = every object with collider you want to be able to be clicked needs a script with On$$anonymous$$ouseDown

Raycast = you can have one script on camera that fires rays then have logic to have reactions on that script if it finds any collider

avatar image timbilodeaugames · Nov 07, 2020 at 08:52 AM 0
Share

The second function is pretty much perfect for my project however how would one limit the range of the raycast? Y'know so I'm not able to click an object from across a room or something.

Also could you elaborate on the hit.transform bit? Would be useful to have the script distinguish between various objects.

In case you hadn't noticed, I am very much a noob!

avatar image
0

Answer by madhu_unity425 · Feb 28, 2019 at 10:13 AM

For Single object is ok , i have 5 cubes

i need only one script but i will use different functions for the these objects

how do i have to write the code for this in one script

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 aldonaletto · Mar 13, 2019 at 07:16 AM 0
Share

Just have five different scripts, each one with On$$anonymous$$ouseDown and the desired function, and assigned to each object.

avatar image
0

Answer by krotovar · Mar 14, 2021 at 06:54 PM

Just in case if you have a 2D project and the previous script doesn't work for you - use this:

 void Update ()
 {
 if (Input.GetMouseButtonDown(0))
         {
             Vector3 clickpos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
             Vector2 click2D= new Vector2(mousePos.x, mousePos.y);
 
             RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero);
             if (hit.collider != null)
             {
                 Debug.Log(hit.collider.gameObject.name);
             }
         }
 }
   
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 scintilla0 · Apr 06, 2021 at 08:33 PM 0
Share

@krotovar
im little bit confused :) origin = current mouse Pos , direction = Vector2.zero so this code should collide eveything on the way(red line) but only hit if i click directly object how is possible. And distance is 0 how is drawing a ray. Btw i also try Debug.DrawRay doesnt help or i couldnt use corretly. You can see codes below.

I drew how am i thought.

alt text

screenshot-2.png (16.0 kB)

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

23 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Click On object that has a larger collider 1 Answer

Get the coordinate of a tap/click and generate an effect on UI 0 Answers

How to make unity expect a click 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