Spawning Prefab


I am looking for some help whit how to spawn prefabs whit scripts. I have tryed to look at some script references, but I can’t seem to understand them, and I would be very happy if someone could take alittle time to explain and help me whit what I am trying to do.

I am making a “Strategy” game, and I am stuck on the part where I want to spawn the different buildings. I have no idea how this works, but everything else that is needed to do this is already done.

I have the prefabs for the buildings, I have the resources scripts and everything else setup do be able to finnish the buildings scripts.

What I need help whit, is to get an sample script on how to spawn the Prefab, and I think I will be able to do the rest my self.

It would be alot of help, if someone could do this for me, and also add explaination on each line so I not only have the code for it, but understand it too!

Thank you for your help,

Best Regards,

Marius Alexander Winsjansen

for spawning prefabs , you want to check out Instantiate :

A suggestion would be to instantiate to a variable, then you have a reference to that object for future use :

public var prefab : GameObject;
private var instObj : GameObject;

// in function
instObj = Instantiate (prefab, Vector3([position]), Quaternion.identity); 

This worked, I entered it into my colliderScript.js and once the enemy object hit my player they would dissapear. Thank you Jay kay.

function OnTriggerEnter (collider: Collider)
    if (collider != null && collider.gameObject.tag == "Player"){
    Destroy(this.gameObject, 2);