- Home /
How do you make an object respond to a click in C#
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);
}
For using this method ("void On$$anonymous$$ouseDown()") object hast have component "Rigidboby"...
To be clear, Here they say that method is "...called when the user has pressed the mouse button while over the GUIElement or Collider".
This Really Helped With $$anonymous$$y Game Development Thanks! ;)
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)
I get it, but where should the second method go? There isn't a universal script we can make, is there?
By the way, to reference the object that was hit, use "hit.collider", not "hit"...
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
?
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
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!
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
Just have five different scripts, each one with On$$anonymous$$ouseDown and the desired function, and assigned to each object.
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);
}
}
}
@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.