Deleting an Object when in-game

So, I know how to delete an object when another object collides with it. But how do I do it so after clicking on a static icon (think of simulation games like simcity and others) it enables my mouse arrow to delete an object when I click on it?

In simcity, there’s usually a bulldozer icon and after clicking it, it enables your mouse arrow to delete something you didnt want in the game scene, that’s what I want to do.

Attach a script to all the objects that can be bulldozed with OnMouseDown:

https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDown.html

Then simply

void OnMouseDown()
    {
        Destroy(gameObject);
    }

Then you can check if the bulldozer is enabled with a bool somewhere. For example a script that contains whether bulldozer is enabled. Then when the Player clicks on the BullDozer icon, set the bool to true. Check this bool before destroying the object eg:

void OnMouseDown()
    {
        if(script.bullDozerIsActive)
        {
            Destroy(gameObject);
        }
    }

If you’re going to have multiple icon selections like BullDozer, Build, etc. Then i would suggest creating a script which controls which one is selected. Then just check which one is selected in OnMouseDown.

It will be after clicking a button that would enable the cursor to delete the stuff. I think I would need OnButtonDown too right?

Yep!

In scene view, go GameObject > UI > Button (Text mesh pro). A canvas will be created if you don’t have one. Then link this button to a script by writing a function in a script, called say ‘EnableDozer’. On your button there is a script where you can link button clicks to scripts and functions in those scripts.

Of all the tutorials ive seen, they are using something that doesnt work anymore on my version of unity. Destroy(gameObject) doesnt work anymore.

What’s your version of Unity? I’m pretty sure Destroy(gameObject) has and always will work.

2018.3

Please post the script where this isn’t working. 2018.3 has been out for a while, and if Destroy was broken in it this wouldn’t be the first we’ve heard of it.

alright. i will work on that tonight. I had to stop that and work on something else thats very important to the game too. I was trying to get isometric to work but I have no idea where the person in the tutorial got her tilemap from because I dont have that whatsoever even though there are the isometric options.

https://www.youtube.com/watch?v=2DsKCJsEzSA

for whatever reason it worked that time.

I think it only worked because I didnt attach the script to the bulldozer button but rather the object that would be destroyed.

Maybe it doesnt consider a button to be a game object.