- Home /
Recieving a nullpointerexception while following the Unity Instantiation tutorial
I am coding this in C#, and I ran into this issue while coding a simple script to shoot a ball from inside of my character. I have figured out it is coming from the AddForce function after casting as a Rigidbody, but can't figure out which value is null.
using UnityEngine;
using System.Collections;
public class Shoot : MonoBehaviour {
public Rigidbody bulletPrefab;
public Transform playerCenter;
void Update() {
if(Input.GetButtonDown("Fire1")) {
Rigidbody bulletSpawn = Instantiate(bulletPrefab, //The prefab spawned
playerCenter.position, //Gets the position the prefab will go
playerCenter.rotation) as Rigidbody; //Gets the rotation the prefab will go
bulletSpawn.AddForce(playerCenter.forward * 5000);
}
}
}
Posting the actual error here would possibly be helpful if smoggach's suggestion wasn't the issue.
Answer by smoggach · Aug 18, 2014 at 06:03 PM
it could be bulletPrefab or playerCenter. Make sure these properties are filled in in your inspector.
Your answer
Follow this Question
Related Questions
AddForce not working 0 Answers
How to decrease inertia for a rolling ball 1 Answer
Object jumping while moving(with addForce) 0 Answers
add force to object that has 2 different rigid bodies 0 Answers
Confusion with AddForce with ForceMode.Acceleration 1 Answer