Simple scripting problem

Hi there!I’ve created a shot code here’s it:

function Update () {
if(Input.GetAxis("Fire1"))
{
shot();
}
}
function shot()
{
var up = transform.TransformDirection(Vector3.up);
   var hit : RaycastHit;    
   Debug.DrawRay(transform.position, up * 10, Color.green);
 
   if(Physics.Raycast(transform.position, up, hit, 10)){
      Debug.Log("Hit");    
      if(hit.collider.gameObject.name == "enemy"){
           Destroy(gameObject.Find("enemy"));
      }
   }
}

This code works pretty fine when I have only one enemy in screen but fails when I put 4 or + enemies,how can I do a code that destruct one enemy per time?

The problem is right here:

Destroy(gameObject.Find("enemy"));

What you are doing is destroying whatever enemy it happens to find, rather than the one you actually hit. Try this:

Destroy(hit.collider.gameObject);

This uses the object that your raycast actually hit, and thus the one you want to destroy.

Oh guy thanks so much:smile: