Camera.main, Camera.allCameras, IndexOutOfBound exception

I was developing a game when a wild error appears. I have the next Hierarchy in my scene:

As you can see, my camera has the tag “MainCamera” that i need to access to it with Camera.main. And i access to it with the next code:

Camera camera = (UseFirstCamera) ? Camera.main : Camera.allCameras[1];

The problem is, with that code i obtain a IndexOutOfBound and I need to use the next code to access the camera:

Camera camera;

if(Camera.allCamerasCount == 1)
	camera = (UseFirstCamera) ? Camera.main : Camera.allCameras[0];
else
	camera = (UseFirstCamera) ? Camera.main : Camera.allCameras[1];

Cause the Camera.main is null, but it shouldn’t be so i think. am I doing or interpreting something wrong with Camera.main variable? (I need to use Camera.allCameras cause in other scenes i have more than one camera).

Is UseFirstCamera returning true? Because otherwise it’ll try and access the Camera.allCameras array. And if there isn’t anything in the ‘1’ index, it will return that IndexOutOfBound error. Have you breakpointed and stepped through to check the values?