- Home /
Instantiate A Prefab
Hey, I am having a little trouble with instantiating.
What I started out with was attaching this script:
var newObject : GameObject;
function Update () {
if (Input.GetButtonDown("Fire1") && Position.y < 0.5) {
Instantiate(newObject, Vector3(0, -2, 0), Vector3(0, 0, 0));
}
}
To the character (3rd person). I then added the "Campfire" prefab as newObject so that if you click the mouse button, it creates a campfire below you. It gave me error messages though, and when you click the mouse, the campfire was located around the character's head and rotated 90 degrees. I need it on the ground.
Does somebody know how I can fix this script to get it to work?
Thanks!
Answer by Mike 3 · Jul 22, 2010 at 08:27 PM
Vector3(0,0,0) in your example should be Quaternion.identity
After that, you should just be able to play around with the first vector to set the right height
Okay, I changed it as you suggested.
But now it places the Campfire in the middle of the map, not where the character is.
Thanks again!
change the vector to (transform.position - Vector3.up), that'll put it one meter below the character's position (you can play with it from there)
I'm not sure if this is right so in a comment but try Instantiate(newObject, transform.position, transform.rotation); And attach the script to the character so that it will instantiate on player. Then you can modify a little by ignoring collision and such. Hope it works/helps :P ! If works give points to $$anonymous$$ike (I owe him anyways) :P