- Home /
Question by
BlackYukirin715 · Aug 17, 2014 at 06:15 AM ·
null reference exception
Bullet Prefab Null reference?
I am trying to make a first person shooting game which my character shoots my bullet Prefab, however, it can't shoot out anything when I press space bar and unity said it was a null reference Exception error. I don't know what to do as a newbie and here below is my code and what should I do?
function Update ()
{
var controller : CharacterController = GetComponent(CharacterController);
// rotate around y - axis
transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed, 0);
// Move forward / backward
var forward = transform.TransformDirection(Vector3.forward);
var curSpeed = speed * Input.GetAxis("Vertical");
controller.SimpleMove(forward * curSpeed);
if(Input.GetButtonDown("Jump"))
{
var bullit = Instantiate(bullitPrefab, GameObject.Find("spawnPoint").transform.position, Quaternion.identity);
bullit.rigidbody.AddForce(transform.forward *2000);
}
}
Comment
Best Answer
Answer by robertbu · Aug 17, 2014 at 06:17 AM
Looking at your code, the most likely problem is here:
GameObject.Find("spawnPoint")
This is likely failing to find the specific game object. Any chance it is named "SpawnPoint" or "Spawnpoint" or "spawnpoint"? Do you have the name mixed up with the tag?
Your answer
