- Home /
Losing reference to the camera after loading new scene
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()
{
DontDestroyOnLoad(transform.gameObject);
}
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 switchthe 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?
Your answer
Follow this Question
Related Questions
Continue playing sound effect on camera after LoadScene 1 Answer
Next LEVEL Load Problem. 2 Answers
DontDestroyOnLoad alternative? 2 Answers
Switching Levels, Instances get destroyed? 2 Answers
Why my Camera is getting Duplicated? 0 Answers