I have a start scene which has a start button. The start scene has a Camera adaptor gameobject. Camera adaptor gets main camera and performs some operations on every scene. When I click on start and the next scene is loaded, the camera adapter object is available in next scene but the camera is missing. here is my code and configuration:
I have added the below code in my CameraAdapter script:
public class CameraAdaptor2D : MonoBehaviour
{
float displayWidth;
float displayHeight;
[SerializeField] Camera cam;
void OnEnable()
{
SceneManager.sceneLoaded += OnSceneLoaded;
}
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
cam = Camera.main;
}
// called when the game is terminated
void OnDisable()
{
Debug.Log("OnDisable");
SceneManager.sceneLoaded -= OnSceneLoaded;
}
private void Awake()
{
DontDestroyOnLoad(this.gameObject);
}
// Start is called before the first frame update
void Start()
{
cam = Camera.main;
displayWidth = Screen.width;
displayHeight = Screen.height;
positionCamera();}
void PositionCamera()
{
Vector3 vector = new Vector3(0.0f,0.0f,0.0f);
cam.transform.position = vector;
}
What happens is when I click on the start button, unity loads the next level. But with the above code, I cannot get the camera of the current scene. Even when I added a breakpoint, the next scene is not pausing at the breakpoint. Is there something that I am missing to add in the code or is there another way to get the cam on every scene?