UCameraComponent Array switch cameras

So I hope no one has posted a solution to this yet as I have been looking for a couple days now.
I have a character that has three CameraComponents as children, each on a spring arm. I’m trying to implement a way to go between them and have been trying this by going through a TArray of all the UCameraComponent objects.
Not sure what I am doing wrong as it has been a very long time since I jumped into C++, but Unreal keeps crashing from my horrible code. Any help or hints would be amazing!

In the constructor:
// Cameras Array Initialize
GetComponents<UCameraComponent>(Cameras);

// THis is the hackiest thing ever, I know.  Just trying to get something working before I clean it up
// Inside a switch camera function
switch (cameraIndex)
	{
	case 0:
		Cameras[0]->SetActive(true);
		Cameras[1]->SetActive(false);
		Cameras[2]->SetActive(false);
		break;
	case 1:
		Cameras[1]->SetActive(true);
		Cameras[0]->SetActive(false);
		Cameras[2]->SetActive(false);
		break;
	case 2:
		Cameras[2]->SetActive(true);
		Cameras[1]->SetActive(false);
		Cameras[0]->SetActive(false);
		break;
	default:
		break;

Sorry for the hideousness of this, I’m very new to Unreal but have been programming C# for years so have a grasp of concepts and ideas, just not syntax.

I don’t know how exactly you want using your cameras, but maybe this help you:

MyCharacter.h

class AMyCharacter : public ACharacter
{
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
	class USpringArmComponent * SpringArm_01;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
	class USpringArmComponent * SpringArm_02;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
	class USpringArmComponent * SpringArm_03;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
	class UCameraComponent * Camera_01;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
	class UCameraComponent * Camera_02;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
	class UCameraComponent * Camera_03;
};

MyCharacter.cpp

AHelpProjectCharacter::AHelpProjectCharacter()
{
	// Create a CameraComponents and SpringArmComponents	
	SpringArm_01 = CreateDefaultSubobject<USpringArmComponent>(L"SpringArm01");
	SpringArm_01->SetupAttachment(GetCapsuleComponent());
	SpringArm_01->SetRelativeLocation(FVector(-39.56f, 1.75f, 64.f));
	SpringArm_01->bUsePawnControlRotation = true;
	SpringArm_01->TargetArmLength = 0.0f;

	Camera_01 = CreateDefaultSubobject<UCameraComponent>(L"Camera01");
	Camera_01->SetupAttachment(SpringArm_01);

	SpringArm_02 = CreateDefaultSubobject<USpringArmComponent>(L"SpringArm02");
	SpringArm_02->SetupAttachment(GetCapsuleComponent());
	SpringArm_02->SetRelativeLocation(FVector(100.0f, 1.75f, 64.f));
	SpringArm_02->bUsePawnControlRotation = true;
	SpringArm_02->TargetArmLength = 0.0f;

	Camera_02 = CreateDefaultSubobject<UCameraComponent>(L"Camera02");
	Camera_02->SetupAttachment(SpringArm_02);
	Camera_02->SetRelativeRotation(FRotator(0.0f, 180.0f, 0.0f));

	SpringArm_03 = CreateDefaultSubobject<USpringArmComponent>(L"SpringArm03");
	SpringArm_03->SetupAttachment(GetCapsuleComponent());
	SpringArm_03->SetRelativeLocation(FVector(-30.0f, 81.0, 64.f));
	SpringArm_03->bUsePawnControlRotation = true;
	SpringArm_03->TargetArmLength = 0.0f;

	Camera_03 = CreateDefaultSubobject<UCameraComponent>(L"Camera03");
	Camera_03->SetupAttachment(SpringArm_03);
	Camera_03->SetRelativeRotation(FRotator(0.0f, -40.0f, 0.0f));
	
	// Create CharacterMesh and attach to Camera_01
	Mesh1P->SetupAttachment(Camera_01);
	//...
}

// When Space is pressed then change camera
void AHelpProjectCharacter::CameraChangePressed()
{
	if (bAllowChangeCamera)
	{
		bAllowChangeCamera = false;

		switch (cameraIndex)
		{
		case 0:
			Camera_01->SetActive(true);
			Mesh1P->SetupAttachment(Camera_01);
			Camera_02->SetActive(false);
			Camera_03->SetActive(false);
			cameraIndex = 1;
			break;
		case 1:
			Camera_02->SetActive(true);
			Mesh1P->SetupAttachment(Camera_02);
			Camera_01->SetActive(false);
			Camera_03->SetActive(false);
			cameraIndex = 2;
			break;
		case 2:
			Camera_03->SetActive(true);
			Mesh1P->SetupAttachment(Camera_03);
			Camera_01->SetActive(false);
			Camera_02->SetActive(false);
			cameraIndex = 0;
			break;
		default:
			break;
		}
	}
}

void AHelpProjectCharacter::CameraChangeReleased()
{
	bAllowChangeCamera = true;
}

This is how looks like my hierarchy:

233889-hierarchy.png

And the final result:

233890-threecameras.gif

When I attach 3 cameras to the character then I see the mesh of this cameras at runtime.

Another way to resolve (I’m not tested this solution):

Create one camera. Remember three transforms and just set one of this Transform to your camera.

switch (cameraIndex)
{
case 0:
	Camera->SetRelativeTransform(Transform_01);
	cameraIndex = 1;
	break;
case 1:
	Camera->SetRelativeTransform(Transform_02);
	cameraIndex = 2;
	break;
case 2:
	Camera->SetRelativeTransform(Transform_03);
	cameraIndex = 0;
	break;
default:
	break;
}

Thanks Shaggy41! This looks great, I’m going to try it out when I get home! I’m not sure what I did to cause my program to crash, but both of your solutions look nice. Thanks for the in depth answer, you are awesome!