how to raycast through some colliders?

I have this script:

var hit : RaycastHit;
		if (Physics.Raycast(rayorigin.position, rayorigin.forward, hit, rayRange)) {
    			if (hit.collider.tag == "Ground Enemy") {
    				hit.collider.SendMessage("TakeDamage", myDamageAmount, SendMessageOptions.DontRequireReceiver);
    				}
    		
    		}

It is for a tower defense and so I have a bunch of towers shooting all really close to eachother and their rays are colliding with eachothers colliders. How do I raycast through colliders so as long as the the enemy is in range it will be getting the message sent?

Create a LayerMask variable and assign in the inspector the layers you want to ignore. You can add multiple layers.

When you cast the ray pass the variable to the method.

Make the objects you want to ignore be in that/those layers.

I will post some code as an answer (I’m not used to UnityScript, though so please excuse any errors):

var hits : RaycastHit[];

hits = Physics.RaycastAll(rayorigin.position, rayorigin.forward, rayRange);

float currentDistance = 10000;

var EnemyColl : Collider;

for(var i = 0; i < hits.Length; ++i)
{
    if(Vector3.Distance(hits_.point, raycastorigin.position) < currentDistance && hits*.collider.tag == "Ground Enemy")*_

{
EnemyColl = hits.collider;
}
}

if(EnemyColl != null)
EnemyColl.SendMessage(“TakeDamage”, myDamageAmount, SendMessageOptions.DontRequireReceiver);
A RaycastAll function returns a list of RaycastHits. In this code you loop through the list you get out of it and get the closest enemy (so that you don’t shoot straight through one) and make that your target enemy. After the looping is done and the EnemyColl is not null, you can send your message. Hope this works!