Applying a force to multiple rigidbodies at once.

Hello all,

I want to script an event where if a first person controller collides with an invisible trigger cube, it applies an AddForce to multiple specified rigidbodies at once, sending them all flying in the direction of the player.

I have the Javascript code working for a single object - the code, attached to the trigger, is below. How would I extend this code to act on multiple rigidbodies at once?

#pragma strict

var target : Collider; // first person controller
var act_on : Rigidbody; // at the moment, a cube
var speed = 10;


function OnTriggerEnter(collision : Collider) 

{

	if (collision != target) //if the colliding object isn't our object...
		{
			return; //don't do anything
		}
	Debug.Log ("Collision");
	act_on.rigidbody.AddForce(Camera.main.transform.forward * (speed * -1));
}

Maybe something like this:

#pragma strict
 
var target : Collider; // first person controller
var act_on : Rigidbody []; // at the moment, a cube
var speed = 10;
 
 
function OnTriggerEnter(collision : Collider) 
 
{
 
    if (collision != target) //if the colliding object isn't our object...
       {
         return; //don't do anything
       }
    Debug.Log ("Collision");
    for (var act:Rigidbody in act_on)
       act.AddForce(Camera.main.transform.forward * (speed * -1));
}

Original code by Oana, edited slightly for this application.

    #pragma strict
     
    var target : Collider; // first person controller
    var act_on : Rigidbody []; // at the moment, a cube
    var speed = 10;
     
     
    function OnTriggerEnter(collision : Collider) {
     
       if (collision != target) //if the colliding object isn't our object...
       {
          return; //don't do anything
       }
       Debug.Log ("Collision");
       act_on = GameObject.FindGameObjectsWithTag("TagName"); //Finds all game objects with said tag and assigns them to move
       for (var act:Rigidbody in act_on){
         act.AddForce(Camera.main.transform.forward * (speed * -1));
       }
   }