My script selects an object with a certain tag but when destroying the object it destroys other objects with the same tag that have not been selected. How could I change this so that it only destroys the object that has been selected? Hope this makes sense.
var selected = false;
var deleteSound : AudioClip;
function Update (){
if(Input.GetMouseButtonDown(0)){
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, hit, 10.0)){
if(hit.collider.tag == "placedObject"){
selected = true;
Wait(hit.collider.renderer);
}
}
}
if(Input.GetKeyDown(KeyCode.C) && selected == true){
var placedObject = GameObject.FindWithTag("placedObject");
Destroy(placedObject);
audio.PlayOneShot(deleteSound);
selected = false;
}
}
function Wait(rend : Renderer){
var originalColor = rend.material.color;
rend.material.color = Color.yellow;
yield WaitForSeconds(2);
rend.material.color = originalColor;
selected = false;
}