unity click on 3d object code example

Example 1: unity click on gameobject

using UnityEngine;

//The gameObject needs to have a collider (if your collider is a trigger, you need to put Physics.queriesHitTriggers = true; in the start function or Physics2D.queriesHitTriggers = true; if you work in 2D)

public class nameOfClass : MonoBehaviour
{

	bool isObjectClicked;
    
	void OnMouseDown() //Detects when you click the gameObject that contains this script
    {
    	isObjectClicked = true;
    	//Your script here
    }
    
    void OnMouseUp() //Detects when you stop clicking on the gameObject that contains this script
    {
    	isObjectClicked = false;
        //Your script here
    }
}

Example 2: unity click object

void OnMouseDown()
{         
	// this object was clicked - do something     
    Destroy (this.gameObject);  
}