Destroy multiple objects user has selected?

What I’m working on right now involves the user selecting ball objects. The user is able to select multiple instances of them. Right now, I’m storing the selection as a boolean variable inside a script for the ball object like so:

var selected = false;

function OnMouseDown ()
	if(selected == false)
		selected = true;

However, I have a game master script and I want to have it so when the user enters a key, it will destroy all the objects that were previously selected.

My initial plan was to tag the object when selected by the user, and then destroy all the objects with the selected tag, but as far as I can tell, you can not tag objects at runtime. Does anyone have an idea for an alternative method?

(Also I’m working in JavaScript in case anyone wants to provide code example.)

You can certainly tag objects at runtime.

  1. Make your tag in the inspector as usual.

  2. When your object is selected, you would just do this: gameObject.tag = “Tag Name”;

  3. When you want to destroy the objects, just do a foreach loop and destroy each one.

Hope this helps