I am trying to access the rigidbody of my camera from my Orb script. Normally when I access a different script, I will create a public static “xxx” instance in that script/class and then I can access it from other scripts using xxx.whateverINeed. But when I try to get the rigidbody using camRB = CameraController.instance.GetComponent(); it throws a null exception error. I’ve done this exact thing (it seems) a hundred other times and it’s never given me a problem. I don’t know why it won’t work. I made sure my camera has a Rigidbody in the Unity Inspector. Please. I’ve been working on this for so long and it doesn’t make sense to me why it’s not working. If anybody has any ideas or links you can provide, please help…
My code:
public class CameraController : MonoBehaviour {
public static CameraController instance;
public Vector3 camOffset;
// Declare Orb handlers
Rigidbody orbRB;
Transform orbTran;
void Start ()
{
instance = this;
orbRB = Orb.instance.GetComponent<Rigidbody>(); // This works fine...
orbTran = Orb.instance.transform;
camOffset = transform.position - orbTran.position;
}
public class Orb : MonoBehaviour {
public static Orb instance;
Rigidbody orbRB, camRB;
Transform orbTran;
Vector3 direction;
// Use this for initialization
void Start()
{
instance = this;
orbRB = GetComponent<Rigidbody>();
//Debug.Log("CameraController.instance =" + (CameraController.instance).ToString());
camRB = CameraController.instance.GetComponent<Rigidbody>(); // This is where I get my error. "NullReferenceException: Object reference not set to an instance of an object Orb.Start() "
}