- Home /
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. ;)
Answer by whydoidoit · Jul 09, 2012 at 05:16 PM
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...
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?
When you specify a position you also need to specify a rotation. Pass Quaternion.identity as the third parameter.
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 program$$anonymous$$g for the first time like, two monts ago and i still have a long way to go. Thanks!!!
Hey no problem :) Just mark it as answered and I'm a happy man! (Click the tick under my answer).