I am getting the error:

MissingReferenceException: The object of type 'Camera' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.

The camera in my game contains a persistent object script, which prevents the camera from being destroyed when loading a new scene.

private void Awake()

The scene is switched via a method in the GameManager:

public void loadScene(UnityEngine.Object arena)
    operation = SceneManager.LoadSceneAsync(arena.name);
    state = GameState.LOADING; 

and finally the camera contains another script which adds a distortion effect:

void OnRenderImage(RenderTexture source, RenderTexture destination)
    distortCam.CopyFrom(mainCam); //Error occurs here
    distortCam.backgroundColor = Color.grey;
    // ...Some more code ...

The error occurs in this last method, but the camera still exists in the editor. mainCam is a Camera object that is assigned onAwake via mainCam = GetComponent<Camera>();

Also if I switch the loadScene method to:

public void loadScene(UnityEngine.Object arena)
    operation = SceneManager.LoadSceneAsync(arena.name, LoadSceneMode.Additive);
    state = GameState.LOADING;

it works perfectly, but it is inconvenient to always have to loadScenes additive.

Why is the reference to the mainCam being lost? I have tried to check if it's null and reassigning it before using it, but so far I still have the problem. Any ideas?

| improve this question | | | | |
  • \$\begingroup\$ Try assigning the mainCam in Start (it runs after Awake) see if that fixes it. \$\endgroup\$ – Vadim Tatarnikov Jun 5 '17 at 12:42
  • \$\begingroup\$ @VadimTatarnikov I tried, but the problem persists. \$\endgroup\$ – Girauder Jun 5 '17 at 15:50
  • \$\begingroup\$ It looks like you have two cameras involved here, mainCam and distortCam. Is it definitely mainCam that is null? I just made a test of CopyFrom(null) and got the message Camera to copy from must not be null instead of the exception. \$\endgroup\$ – Victor T. Jun 5 '17 at 16:20
  • \$\begingroup\$ Thanks you're actually right. It was not the mainCam that was being deleted but the distortCam which was created in the script and not being added to the persistentObjects \$\endgroup\$ – Girauder Jun 5 '17 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.