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);
}
}
}
↧