x


How to instantiate in self position.

Ok guys, my first post here on this website. I know it's kind of a noob question but, either i'm not looking from what is in front of my eyes, or.. i don't know.

So, i want to instantiate a prefab in the same position of a trigger when i enter it.

And i have this:

var firePrefab: Transform;

function OnTriggerEnter (other : Collider)
{
 Instantiate(firePrefab, gameObject.position);
}

So i presumed that if i attached the script to my object it would instantiate it where in the same position of my gameObject to where's the script is attached. Tommorow this will become a onCollisionEnter so that the script can be attached to a bullet and where the bullet hits, it destroys itself and instantiate my prefab, wich goes for 5 seconds and then disappeas, well that i know how to do with destroy and waitforSeconds commands. Can you solve this for me please? Thanks.

P.S. - Oh, and since i'm a noob with unity, keep it simple and stupid please. :D

P.S._2 - No need for raycasts here, the bullet is a rigid body since, well, is not a bullet, more like a missile to be shoot from a tank. :D Cheers. ;)

more ▼

asked Jul 09 '12 at 05:14 PM

Satamanster gravatar image

Satamanster
19 1 6 11

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

1 answer: sort voted first

use #pragma strict at the top of your files and you will see that gameObject.position doesn't exist! You need to be using transform.position...

more ▼

answered Jul 09 '12 at 05:16 PM

whydoidoit gravatar image

whydoidoit
33.7k 14 23 105

I already tried that:

Look, here's what i have now:

pragma strict

var firePrefab: Transform;

function OnTriggerEnter (other : Collider) { Instantiate(firePrefab, gameObject.transform.position); }

I tried with just transform.position and gameObject.transform.position and nothing works.

But I'm still getting this error. BCE0023: No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(UnityEngine.Transform, UnityEngine.Vector3)' was found.

Shouldn't the gameObject.transform.position returns a vector3 by itself? What i'm i doing wrong?

Jul 09 '12 at 05:35 PM Satamanster

When you specify a position you also need to specify a rotation. Pass Quaternion.identity as the third parameter.

Jul 09 '12 at 05:37 PM whydoidoit

Thanks... You are my saver. :D i actually tought rotation was a optional and if i didn't put anything ther it automatically would assume the Quaternion.identity vector. : Weird. Well, thanks a lot, i've just been intro unity scripting and programming for the first time like, two monts ago and i still have a long way to go. Thanks!!!

Jul 09 '12 at 06:04 PM Satamanster

Hey no problem :) Just mark it as answered and I'm a happy man! (Click the tick under my answer).

Jul 09 '12 at 06:06 PM whydoidoit
(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:

x1725
x1286
x915
x60
x13

asked: Jul 09 '12 at 05:14 PM

Seen: 473 times

Last Updated: Jul 09 '12 at 06:06 PM