Detect Object that are hit by a ray

I have this script which casts a ray when the mouse button is clicked

var Bullet1_direction = Vector3(15,0,45);
var Bullet2_direction = Vector3(0 ,0,1);
var Bullet3_direction = Vector3(-9 ,0,45);
var length = 4;

function Update () {

  var Bullet1_diagonal = transform.TransformDirection(Bullet1_direction);
  Bullet1_diagonal.Normalize();

  var Bullet2_diagonal = transform.TransformDirection(Bullet2_direction);
  Bullet2_diagonal.Normalize();

  var Bullet3_diagonal = transform.TransformDirection(Bullet3_direction);
  Bullet3_diagonal.Normalize();

  if(Input.GetMouseButtonDown(0)){

    Debug.DrawRay(transform.position, Bullet1_diagonal * length,Color.red);
    Debug.DrawRay(transform.position, Bullet2_diagonal * length,Color.red);
    Debug.DrawRay(transform.position, Bullet3_diagonal * length,Color.red);

    if (Physics.Raycast(transform.position, Bullet1_diagonal,length)) {
      Debug.Log ("Bullet 1 hit red!");
    }
    if (Physics.Raycast(transform.position, Bullet2_diagonal,length)) {
      Debug.Log ("Bullet 2 hit red!");
    }
    if (Physics.Raycast(transform.position, Bullet3_diagonal,length)) {
      Debug.Log ("Bullet 3 hit red!");
    }
  }

}

what i want to do it make it so what ever object the ray hits it returns the name of the object and destroys that object. for example im using it for a top down ish shooter game so the object it hits will be an enemy however there will be more then one of the same enemy in the scene at once so can you make it that what ever object the ray hits to destroy that singular ibect not every one of thoes object?

help would be awesome

you can use an out RaycastHit as one of the parameters for Physics.Raycast (look at its overloaded options) Then

if(Physics.Raycast())
{
 hit.collider.gameObject <---- the game object that was hit.
}

@ Bryan this is how im interpretation the code you gave me

 if (Physics.Raycast(transform.position, Bullet1_diagonal,length)) {
 if(Physics.Raycast(hit.collider.gameObject)){
 if(gameObject.tag == ("Enemy")){   
Debug.Log ("kill");

 }
 }
 }

except i get a null reference error Help?

Well from what i read i hope is correct you have multiple enemies right and when the ray collides with the bad guy you want to destroy the game object. Now the easiest way that i can think of is creating variables at the top which carry gameObject in this case your enemies. every time your ray collides variable set gameObject with the enemy in it the ray will destroy the gameObject with the right tag. here is what i would do:

    var Bullet1_direction = Vector3(15,0,45);

    var Bullet2_direction = Vector3(0 ,0,1);

     var Bullet3_direction = Vector3(-9 ,0,45);

      var length = 4;

        var Enemy1 : GameObject;

          var Enemy2 : GameObject;

          var Enemy3 : GameObject;

       function Update () {

              var Hit : RaycastHit;

       var Bullet1_diagonal = transform.TransformDirection(Bullet1_direction);

       Bullet1_diagonal.Normalize();

      var Bullet2_diagonal = transform.TransformDirection(Bullet2_direction);

       Bullet2_diagonal.Normalize();

      var Bullet3_diagonal = transform.TransformDirection(Bullet3_direction);

        Bullet3_diagonal.Normalize();

       if(Input.GetMouseButtonDown(0)){

       Debug.DrawRay(transform.position, Bullet1_diagonal * length,Color.red);

            Debug.DrawRay(transform.position, Bullet2_diagonal * length,Color.red);

                Debug.DrawRay(transform.position, Bullet3_diagonal * length,Color.red);

     if (Physics.Raycast(transform.position, Bullet1_diagonal,Hit,length)) {

          Debug.Log ("Bullet 1 hit red!");

            if(Hit.collider.gameObject.tag == "Enemy1"){

                Destroy(Hit.collider.gameOject); 

     }

    }

  if (Physics.Raycast(transform.position, Bullet2_diagonal,Hit,length)) {

      Debug.Log ("Bullet 2 hit red!");

         if(Hit.collider.gameObject.tag == "Enemy2"){

            Destroy(Hit.collider.gameOject); 

      }

         }

    if (Physics.Raycast(transform.position, Bullet3_diagonal,Hit,length)) {

      Debug.Log ("Bullet 3 hit red!");

         if(Hit.collider.gameObject.tag == "Enemy3"){

          Destroy(Hit.collider.gameOject); 

          }

       }

    }

 } 

Now the way that this works is really simple if the ray hits the following enemy stored in the gameObject it will destroy it. don't forget to assign the game object in your inspector mode. If this does not work please let me know thanks :)


Problem Update :


i have solved your problem :) basically the problem was in the ray itself, this is because if you remember on the top of update i created a variable called var Hit: RayCastHit

this became a major part in your ray because every time it would shoot the ray out i was told to destroy an object that was stored in the hit variable. So all i need to do was add the following to the following line:

  if (Physics.Raycast(transform.position, Bullet1_diagonal,Hit,length)) {

      Debug.Log ("Bullet 1 hit red!");

        if(Hit.collider.gameObject.tag == "Enemy1"){

            Destroy(Hit.collider.gameObject); 

i have pasted the code that had the changes you will now notice that after diagonal i have placed "HIT" Now we have told the ray once you shoot out your diagonal bullet look for the stored object in the hit. And then at the bottom we have just stored the hit to enemy 1 and then destroy the object. now the second thing you may find different is the Destroy call instead of using the 3 var gameobjects at the top i just told it to destroy the object that is collided with the hit in this case an Enemy that has the tag "Enemy1"

hope it helps :)