- Home /
object spawn on trigger
i have this script and im trying to work in a line that will generate a game object when the player passes through a trigger but in not sure what im doing wrong.
var sendToObject: GameObject; // Add in the step2 var correctObject: GameObject; //this is the only object we allow to trigger the event var targetFunction : String; // the name of the function to call
function OnTriggerEnter (object : Collider) { //print (object.name); // this is the name of the object triggered the event if(object == correctObject.collider) ModelImporterGenerateMaterials: GameObject; }
function OnTriggerExit (object : Collider) { //print (object.name); // this is the name of the object triggered the event if(object == correctObject.collider) sendToObject.SendMessage("targetFunction"); }
im positive my problems are steming from this line if(object == correctObject.collider) ModelImporterGenerateMaterials: GameObject; i just dont know what wording i need in order for it to spawn the gameobject
Answer by kevork · Feb 03, 2012 at 01:38 AM
You want to use Object.Instantiate. More information can be found here Instantiating Prefabs.
ok i checked out those pages and this is what my script looks like now
var sendToObject: GameObject; // Add in the step2
var correctObject: GameObject; //this is the only object we allow to trigger the event
var targetFunction: String; // the name of the function to call
var myBox: Transform;
function OnTriggerEnter (object : Collider) { //print (object.name); // this is the name of the object triggered the event
if(object == correctObject.collider) var instanceObj = Instantiate(myBox, Vector3(23, 0, -3), Quaternion.identity);
}
function OnTriggerExit (object : Collider) { //print (object.name); // this is the name of the object triggered the event
if(object == correctObject.collider) sendToObject.Send$$anonymous$$essage("targetFunction");
}
the thing is when i run the game my box prefab is nowhere to be seen however i get no errors...