Can not access Rigidbody of my camera

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() "

    }

I think the Orb script gets loaded first and it tries to access the CameraController instance before Start is called on that script. Try changing the script execution order so the CameraController is first (smaller numbers will execute first).

As @JedBeryll said, the problem is with the script execution order. I personally prefer to use the setting of the Script Execution Order, as a last resort, i.e. if nothing else works, and it most cases, something else does work.

In this particular case, create the CameraController instance inside Awake():

 public class CameraController : MonoBehaviour {
 
     public static CameraController instance;
     public Vector3 camOffset;
     // Declare Orb handlers
     Rigidbody orbRB;
     Transform orbTran;   

void Awake(){

    instance = this; //the instance will now be available when Orb.Start() runs (as long as the camera is active in the scene, when Orb script loads.

}
 
     void Start ()
     {
         orbRB = Orb.instance.GetComponent<Rigidbody>(); // This works fine...
         orbTran = Orb.instance.transform;
         camOffset = transform.position - orbTran.position;
     }

//etc...
}

In a more general case, this would present a problem, if a 3rd script depended on an instance that has to be initialized in Start() too, because it, itself depends on another instance that gets initialized in Awake(). e.g. CustomScript.Start() needs a reference to Orb.instance, but it happens that CustomScript executes before Orb.

Yes, you could set the Execution Order to set CustomScript to execute after Orb. However, I would prefer to use a public variable in CustomScript to get a reference to Orb by dragging and dropping it in the Inspector.

Please also note that (if my memory serves me well) the order of execution of scripts may change (if not explicitly set). I believe that I’ve seen several cases, where script1 would execute before script2, and then suddenly, say after some change in the project, like adding more scripts, script2 would execute before script1. I haven’t seen any documentation on this, it’s just a matter of observation.
So, what this means is, that you may currently be able to reference instances from within Start(), but this could probably break in the future, if you initialize all instances in Start().

It’s an issue that needs a lot of care.