Game instance not initializing, causes crash

Accessing complex variables in Game Instance causes a crash, I think because they are not being initialized. I have a custom game instance class set up with a custom variable:

MyGameInstance.h

...

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Custom)
		float BoxLimit;

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Custom)
		TArray<float> X_Main;

...

I try to assign values in a custon UActorComponent (at begin play, not in the constructor as I know this can cause isses) as follows:

void UMyActorComp::BeginPlay()
{
	Super::BeginPlay();

	UMyGameInstance* gameInstance = (UMyGameInstance*)(((UGameEngine*)GEngine)->GameInstance);
	if (!gameInstance) return;
	gameInstance->BoxLimit = 33.40;
	UE_LOG(LogTemp, Warning, TEXT("Result: %f"), gameInstance->BoxLimit);
	gameInstance->X_Main.Add(22.3);
	UE_LOG(LogTemp, Warning, TEXT("Result: %f"), gameInstance->X_Main[0]);
	// ...
	
}

This causes a crash. Interestingly, if I comment out the last two lines here, the code works. I think this is because the TArray for X_Main is not properly initialized, wheras the float is just reserved memory, so it’s fine. I’ve been having huge problems getting any kind of default values to be created in my game instance, and I think this might be connected. The game instance is set in maps and modes.

Many thanks.

try this → before adding items
check if the last index is valid, if not then add item to index 0.