Camera.main error


Can you help me?

Hi Pito,

Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.

You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.

What error did you get? A NullReferenceException? If so, check if your camera game object in your game has got the “Main Camera” tag assigned.

Hope this helps :slight_smile:


See also;

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.

This is the error. My camera’s tag is ‘MainCamera’. Here is the code.

[SerializeField] private int coinScore = 1;

[SerializeField] private AudioClip coinPickUpSFX;

private void OnTriggerEnter2D(Collider2D other)
{
    FindObjectOfType<GameSession>().AddToScore(coinScore);
    AudioSource.PlayClipAtPoint(coinPickUpSFX, Camera.main.transform.position);
    Destroy(gameObject);
    
}

Check the other game objects. Only the camera is allowed to have the “Main Camera” tag. And there must be only one Camera component in the scene.

If that’s the case, check if the “Camera” gets destroyed during runtime.

Everything is untagged.

[SerializeField] private int coinScore = 1;
[SerializeField] Camera camera;
[SerializeField] private AudioClip coinPickUpSFX;

private void OnTriggerEnter2D(Collider2D other)
{
    FindObjectOfType<GameSession>().AddToScore(coinScore);
    AudioSource.PlayClipAtPoint(coinPickUpSFX, camera.transform.position);
    Destroy(gameObject);
}

I write like that. But when i die my camera disappears.

Does anything destroy the camera? What happens if your player dies? Maybe the new player does not have a reference to the camera.

It is happening when player dies. I am using my code what I sent you. I don’t understand why camera is disappear.
image

Do you have any persistent game objects in your Hierarchy? Or is there a script that might destroy the camera?

GameSession and ScenePersist don’t destroy. When player die, it load same scene again and my old camera destroy. There is new camera so the camera what in coins is missing.

Are the coins persistent? If so, that would explain why the reference to the camera gets lots when the scene gets reloaded. All game objects, except for the persistent ones, in the current scene get destroyed when a new scene gets loaded.

Coins are persistent. I know everything get destroyed but why camera.main is giving an error? How can I solve that the missing camera thing with my own code?

The problem is the following: The camera is referenced in the “current” scene but it is not persistent. This means that it gets destroyed if a new scene gets loaded or if the current scene gets reloaded. The reference in your persistent objects gets lost.

Solution: When a new scene gets loaded, your persistent object has to look for a camera object again.

The onSceneLoaded delegate might be helpful. Take a look at the example in the API and try to apply it in your own project:


See also:

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms