click delete

Hello
How am I able to make a script that when you click an object in the game and press delete it deletes.
Thanks

Just out of curiosity, did you ever get your other problem solved?

As for this question, if you want to separate the ‘clicking on’ and ‘deleting’ steps, you’ll probably need to come up with a way to track (and maybe visualize) which objects are ‘selected’. How to do that is really up to you, but here’s one way you could do it:

  1. Create a Selectable component with a Boolean ‘selected’ member variable; attach this script to any object that you want to be ‘deletable’.

  2. Attach colliders to these objects as well (if they don’t already have them).

  3. In the Selectable script, add an OnMouseDown() function, and set the ‘selected’ flag to ‘true’ in this function.

  4. In the Selectable script, add an Update() function, and in this function use Destroy() to remove the object if the delete key is pressed and the object is selected.

How to represent visually that an object is selected has been discussed before; try searching the forums for ‘highlight object’.

The above of course doesn’t implement all the features one might expect from a selection system (e.g. selecting one item deselects other items, shift-selecting selects multiple items, etc.), so if you need behavior beyond what you described in your post, additional logic will most likely be required.

Hello
I did not solve it yet
thanks

try this

Void OnMouseDown()
{
         Destroy(gameObject);
}

or

Void OnMouseDown()
{
          gameObject.active = false;
}

The OP stated that the object should be deleted once it’s been clicked on and the delete key has been pressed, which isn’t what your code does.

Also, it’s ‘void’, not ‘Void’.