How to attach a script to an instantiated object?

Hi there. I’ve searched through many questions around here and cannot seem to get any of the script fragments I find to work in my favor. Here is part of the JS script I currently have set up:

var z : Transform;

function OnTriggerEnter(hit : Collider){
Instantiate(z, Vector3(3032.3,2781.8,3068.6),Quaternion.identity);
z = gameObject.AddComponent("yesz");
}

The instantiation of the object z works out just fine, but the script “yesz” never gets attached to it. Could someone tell me what I should do? Thank you.

Instantiate returns the instance you just created, and it’s that you want to attach the component to, not your prefab “z”.

var z : Transform;

function OnTriggerEnter(hit : Collider){
    var objectYouCreate = Instantiate(z, Vector3(3032.3,2781.8,3068.6), Quaternion.identity);
    objectYouCreate.AddComponent("yesz");
}

Kudos go to tanoshimi:

function OnTriggerEnter(hit : Collider){
var objectYouCreate = Instantiate(z, Vector3(3032.3,2781.8,3068.6),Quaternion.identity);
objectYouCreate.gameObject.AddComponent("yesz");