Changing aspects of a function mid play...

Ok, so thanks to you guys I got my last issue sorted..so hopefully you can help me on this one too.

I need to change the value of a function on the occurrence of an event happening in the game.

function calc () {
  var randomize    = Vector3((Random.value *1) -1, (Random.value * 2) -1, (Random.value * 1) -1);

  randomize.Normalize();
  randomize *= randomness;

  flockCenter = Controller.GetComponent("Boid Controller Flock 1").flockCenter;
  flockVelocity = Controller.GetComponent("Boid Controller Flock 1").flockVelocity;
  follow = chasee.transform.localPosition;

  flockCenter = flockCenter - transform.localPosition;
  flockVelocity = flockVelocity - rigidbody.velocity;
  follow = follow - transform.localPosition;

  return (flockCenter+flockVelocity+follow*2+randomize);

}

This is the code I have... I need to have flockCenter and flockVelocity change the script it's looking for. Problem is that the script is attached to another gameobject, how would I reference this gameobject? as I'm guessing the rest of the line would read very similar, just with the corrected script name...

Hopefully you can help, would not only be great for my game, but would also help me understand javascript a hell of a lot more!

Instead of looking up the other gameobject by name as you do now (Controller.GetComponent("Boid Controller Flock 1")), (which is very slow by the way), you can add a public variable to your script, of type GameObject. This variable now shows up in the editor, and you can drag any gameobject on it that you want. A reference to that gameobject will then be put into your variable by Unity before your script starts.

var myBoid:GameObject;

A variable like that can also be changed based on some event. You can change it with script to be something else.

The getcomponent refers to the script, the controller refers to the object, so its only looking through that object..not everything, if I'm right. so its not too much of a performance hog. Because of this,I don't think your solution would solve my problem. I think I could be clearer though.. basically the controller and everything is already changing...

I just need to have the function look for a different script. The whole line EG ...

flockCenter = Controller.GetComponent("Boid Controller Flock 1").flockCenter;

can remain the same, I just want it to look for "Boid Controller" instead, as thats the name of the script applied to the changed controller...

hopefully that makes things simpler.

I've added an if statement inside my function, so it changes the name of the script that it would refer to.. this is what I've done

function calc () {

var randomize = Vector3((Random.value *1) -1, (Random.value * 2) -1, (Random.value * 1) -1);
randomize.Normalize();
randomize *= randomness;

         if(Input.GetKeyUp("c"))

{
flockCenter = Controller.GetComponent("Boid Controller").flockCenter;
flockVelocity =  Controller.GetComponent("Boid Controller").flockVelocity;
}

else

{
flockCenter = Controller.GetComponent("Boid Controller Flock 1").flockCenter;
flockVelocity =  Controller.GetComponent("Boid Controller Flock 1").flockVelocity;
}

the script still works fine, but when I press C, I get the error that this line

flockCenter = Controller.GetComponent("Boid Controller Flock 1").flockCenter;

Object reference not set to an instance of an object

it seems that its not updating to look for the new script, and continues looking for the old script..