x


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!

more ▼

asked Jul 22 '10 at 08:14 PM

MythStrott gravatar image

MythStrott
176 22 23 31

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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

more ▼

answered Jul 22 '10 at 08:27 PM

Mike 3 gravatar image

Mike 3
30.5k 10 65 252

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!

Jul 22 '10 at 08:40 PM MythStrott

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)

Jul 22 '10 at 09:09 PM Mike 3

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 Mike (I owe him anyways) :P

Jul 22 '10 at 09:14 PM MikezNesh

oh well beaten by Mike.

Jul 22 '10 at 09:14 PM MikezNesh

I got it to work. Thanks!

Jul 23 '10 at 04:19 AM MythStrott
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3320
x2075
x1670
x1252
x120

asked: Jul 22 '10 at 08:14 PM

Seen: 1487 times

Last Updated: Jul 22 '10 at 08:14 PM