- Home /
Bullet Spawning Problem
I'm trying to get a bullet to spawn at the location of my game object called "BULLETSPAWN" with the same rotation. I'm trying to use the code:
var bulletPrefab : Transform;
function Update () {
if(Input.GetButtonDown("Fire1"))
{
var bullet = Instantiate(bulletPrefab,GameObject.Find ("BULLETSPAWN"),Quaternion.identity);
}
}
But I get the error:
Assets/BULLETSHOOT.js(13,28): BCE0023: No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(UnityEngine.Transform, UnityEngine.GameObject, UnityEngine.Quaternion)' was found.
Any suggestions?
Answer by fafase · May 01, 2012 at 05:18 PM
try with
var bullet = Instantiate(bulletPrefab,GameObject.Find ("BULLETSPAWN").transform.position,Quaternion.identity);
The second argument is the position but you are passing the whole game object hence the error.
Also if bulletspawn is a child of the obejct use:
var bullet = Instantiate(bulletPrefab,transform.Find ("BULLETSPAWN").transform.position,Quaternion.identity);
It will look inside the object instead of going through all objects in the game. Way faster.
Hmm this seemed to work until I tried to shoot in game, at which point it gave me the error: NullReferenceException
Just make the bulletspawn a public transform, and then use 'bulletSpawn.position'. You should never have to use GameObject.Find(string) for anything.
Your answer
![](https://koobas.hobune.stream/wayback/20220613070949im_/https://answers.unity.com/themes/thub/images/avi.jpg)