If you want to destroy a game object by tapping in Unity, follow these steps! Since the New Input System is a default choice for my tutorials, I will first teach you how to implement a simple system for tapping and clicking in Unity.
If you already have the new input system package installed in Unity, all you have to do is to add a new input actions asset, generate C# class and add a new layer called "Click". Inside that action layer, add an action called "Touch". This action will include only two bindings: Mouse button and screen tap.
At first, you need to make the introduction to the new input system. You can do it in void Awake(), simply by saying input = new InputSystem().
Now it is time to add the input action. Do it simply by adding input.Click.Touch.performed. You need to define what action do you want to be performed. It will be called OnTap. Make sure to put a Callback Context, and an if statement that checks if you click on the screen.
The parts of the script are defined with raycasting. You need to cast the screen to world point if you want to instantiate the game object that will symbolize the cursor. The reason why you also need a circle object is because it will be an aiming cursor every time you click on the screen.
The void OnMouseDown() is still considered old input system, but it can be useful for detecting if you are tapping the object. Inside that void, simply put Destroy(gameObject) and that will work fine.
Support the Creator of Kozmobot on the social networks!
CHECK OUT MY WEBSITE: http://kozmobot.com
INSTAGRAM: / kozmobot.channel
TIKTOK: / kozmobot.channel