How can I add a script to a gameobject with a script?

I want to add a predefined script to a gameobject when the player collides with it. What's the proper javasript syntax to make that happen? I know how to use OnTriggerEnter, but I don't know how to modify the gameobject with script so that the predefined script I want to add to it will become a component of that gameobject. Thanks!

The following code adds the script named FoobarScript.js to your gameobject and returns a reference to it in variable fbs (so you can set variables in the script with it):

var fbs = gameObject.AddComponent(FoobarScript);

you should use the AddComponent function. simply write a code like this:

var superMe = gameObject.AddComponent (newscriptname); //where newscriptname is the name of the new component that you want to add.

you can use Destroy to remove a component from a gameObject.

Destroy (rigidbody); //removes the rigidbody component