Why when switching back to main camera i'm getting on game view error: No cameras rendering ?

The script is attached to player first person controller.

When i enter to the event OnTriggerEnter it’s switching to cam without a problem.
But on the event OnTriggerExit i’m getting the error.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnterExitSpaceship : MonoBehaviour
{
    private Camera cam;

    private void OnTriggerEnter(Collider other)
    {
        cam = GameObject.Find("Spaceship Camera").GetComponent<Camera>();
        Camera.main.enabled = false;
        cam.enabled = true;
    }

    private void OnTriggerExit(Collider other)
    {
        cam.enabled = false;
        Camera.main.enabled = true;
    }
}

Just create a reference to the main camera and assign Camera.main to it in Start. You should be doing that with your spaceship camera also as GameObject.Find is generally not the best way to do things anyways. (you could even tag the spaceship cam and use the FindWithTag instead), but I would still do it in start.

Camera.main has some things about it where I think it searches for the camera and returns it, so since you are disabling it, I don’t think it will find it when you try to turn it back on.

1 Like

Working thanks.