Hi. I want to destroy objects if they are clicked. I know that I have to use Raycasts for that. I don’t want to detect clicks on a single object. I need to detect it on all objects tagged with “destroyable”. So I’ll also need GameObject.FindGameObjectsWithTag(“destroyable”). Now I have no idea how to check all the tagged objects.
You don’t have to find all game objects with the tag. As you click on something, you can check it tag. A script (untested):
function Update () {
if (Input.GetMouseButtonDown(0)) {
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit)) {
if (hit.collider.tag == "destroyable") {
Destroy(hit.collider.gameObject);
}
}
}
}