Instantiate an Object to a specific position?

for my game I’m making it so you walk into a trigger, a sound plays, you turn around and there is something there. I have managed to get the object to spawn but it is way outside my map, how can I set it to the position I want? Here is the script.

var prefab : Transform;
private var soundPlayed : boolean = false;
var Laugh : AudioClip; 
var volume: float = 10.45;
var LightOff : AudioClip; 
var ObjToDestroy : Light;
var ObjToDestroy2 : Light;
var ObjToDestroy3 : Light;
var ObjToDestroy4 : Light;
var ObjToDestroy5 : Light;
var ObjToDestroy6 : Light;
var ObjToDestroy7 : GameObject;


function OnTriggerEnter(col : Collider){ //Play Sound if player enters trigger
  if (!soundPlayed && Laugh) { // but only if it has not played before
    AudioSource.PlayClipAtPoint(Laugh, transform.position, 10.4);
    soundPlayed = true; // sound will not play anymore
    audio.PlayOneShot(LightOff); 
   Destroy (ObjToDestroy);
   Destroy (ObjToDestroy2);
   Destroy (ObjToDestroy3);
   Destroy (ObjToDestroy4);
   Destroy (ObjToDestroy5);
   Destroy (ObjToDestroy6);
   Destroy (ObjToDestroy7);
Instantiate (prefab);
}
    }

to instantiate obj at a specific point, just add to specific location to the script.

Here’s how it’s done!

Instantiate (prefab, Vector3 (0,0,0), transform.rotation);

or you can make an empty game object. Place it somewhere in the scene whici you want to prefab to instantiate at that.

Then in ur script :

var theLocation : Transform;
Instantiate (prefab , theLocation.position, transform.rotation);

Hope this is what you are looking for

Instatiate(prefab, whereYouWantToSpawn, Quaternion.identity);

Replace whereYouWantToSpawn with a Vector3 with the x,y,z of where you want your object.

Thanks heaps! :slight_smile: