Gameobject click detect

I have game object here where inside that game object I have 2D sprite and I have this script attach to gameobject. But when I click the sprite it won’t destroy why ??, sorry for asking thi snewbie questio. I’m stilll new and learning this unity. Hope u guys can teach me :slight_smile:

using UnityEngine;
using System.Collections;

public class TakePhoto : MonoBehaviour {
    void OnMouseDown() {
        Destroy(gameObject);
    }
}
1 Like

Do you have a collider on the object?

Hhmm I think no, can u explain how to make the collider and where I put in ?? Thanks

Click on the GameObject. Click on AddComponent. Type in collider or collider2d. Choose one as appropriate.

Note that OnMouseDown is an old method. Strictly speaking its not legacy. But you might want to consider using the EventSystem instead to make things nicer with your UI. You can always change it later, so no biggie.

Event system like at the UIsystem ?? How event system work and why eventsystem is better ??

The EventSystem works by gathering all of your inputs into one place first before sending them out to your various objects. I’m not sure if its objectively better. But it does make working with the UI easier as it will automatically prevent clicks from the UI system touching your in game system.

Ooh that’s mean we script all user input in 1 script to detect all the 2d sprite, button or something click. Not at different script where I make my script function on mouse down 1 by 1. Like that ??

Your script would look like this.

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
 
public class TakePhoto : MonoBehaviour, IPointerClickHandler {
    void OnClick() {
        Destroy(gameObject);
    }
}

You would also need an event system in the scene, an appropriate physicsraycaster on your camera, and an appropriate collider on your gameobject

1 Like

Physicraycaster at camera and collider at my gameobject ok, so that mousedown event script I only attach it at gameobject right ??

Yup.

oh wait2 so 1 I must UI > Eventsystem then I put the collider and physicraycaster in this event system or, I put my maincamera > physyc raycater and gameobject > sprite and collider in the event system ??

EventSystem has the EventSystem component
Main Camera has the PhysicsRaycaster
GameObject has the Sprite, Collider and your script

hmm ok I’ll try it. SOrry for to much asking newbie question here. Hope u’re not tired to give me explanation

I get error message with IPointerClickHandler, when I write the code u give above. here is my properties, check it at jpg there. Am I doing it wrong ??



SOrry for much picture there. my first time upload picture here idk if it allready upload

There’s no sprite attached to the gameobject you’re attempting to add the collider to.

1 Like

see this, there’s a sprite shoot there

2235199--148984--123.jpg

Is it OnClick event same with OnMouseDown event at android system ?

first, add:
using UnityEngine.UI;

on the object add a “collider”

the script added to the object can detect a mouse with the function :
void OnMouseDown() {

This function will then turn to another color.

Bad idea these days. You want to implement the IPointerClick interface instead.

OnMouseDown is the old way of doing it. Its not compatible with the UI system, and will cause problems. Its only still included for backwards compatibility.

1 Like