Array index out of bounds: 1 from an array of size 1 Crash

I have a function which returns a random UAnimMontage from an array. The code is scuffed, but I’m not sure how else to implement it.

if (AttackDirection == EWeaponAttackDirection::AD_InAir)
{
const uint8 RandomInt = FMath::RandRange(0, InAirAnimations.Num());
return InAirAnimations[RandomInt];
}

if (AttackDirection == EWeaponAttackDirection::AD_Down)
{
	const uint8 RandomInt = FMath::RandRange(0, DownAnimations.Num());
	return DownAnimations[RandomInt];
}

else if (AttackDirection == EWeaponAttackDirection::AD_Up) //Up animations and thrust animations are one and the same
{
	const uint8 RandomInt = FMath::RandRange(0, ThrustAnimations.Num());
	return ThrustAnimations[RandomInt];
}

else if (AttackDirection == EWeaponAttackDirection::AD_Left)
{
	const uint8 RandomInt = FMath::RandRange(0, LeftAnimations.Num());
	return LeftAnimations[RandomInt];
}

else if (AttackDirection == EWeaponAttackDirection::AD_UpLeft)
{
	const uint8 RandomInt = FMath::RandRange(0, TopLeftAnimations.Num());
	return TopLeftAnimations[RandomInt];
}

else if (AttackDirection == EWeaponAttackDirection::AD_BottomLeft)
{
	const uint8 RandomInt = FMath::RandRange(0, BottomLeftAnimations.Num());
	return BottomLeftAnimations[RandomInt];
}

else if (AttackDirection == EWeaponAttackDirection::AD_Right)
{
	const uint8 RandomInt = FMath::RandRange(0, RightAnimations.Num());
	return RightAnimations[RandomInt];
}

else if (AttackDirection == EWeaponAttackDirection::AD_BottomRight)
{
	const uint8 RandomInt = FMath::RandRange(0, BottomRightAnimations.Num());
	return BottomRightAnimations[RandomInt];
}

else if (AttackDirection == EWeaponAttackDirection::AD_UpRight)
{
	const uint8 RandomInt = FMath::RandRange(0, TopRightAnimations.Num());
	return TopRightAnimations[RandomInt];
}


return ThrustAnimations[0];

This is called by another function:

if ((ComboCount >= 1) && (CurrentAttackDirection == PreviousAttackDirection))
{
	//Do spin animations here, currently it is still regular animations
	if (UAnimMontage* SpinAnimation = GetRandomMontage(CurrentAttackDirection))
	{
		return SpinAnimation;
	}
}

if (UAnimMontage* RegularAnimation = GetRandomMontage(CurrentAttackDirection))
{
	return RegularAnimation;
}
return ThrustAnimations[0];

The latter function is executed by the character when they melee, and I play the anim montage in blueprints (Specifically within a GAS Ability). In the latter function, if I delete the if statements and only return ThrustAnimations[0], there is no crash and it successfully plays the animation. The array it throws is “appError called: Assertion failed: (Index >= 0) & (Index < ArrayNum) [File:F:\Unreal Engine\Installs\UE_5.1\Engine\Source\Runtime\Core\Public\Containers\Array.h] [Line: 763]
Array index out of bounds: 1 from an array of size 1”. I thought that it may be that the array index is of size one, meaning that index 1 would be invalid, and it should instead be index 0. So, I decided to subtract 1 from the random integer mentioned in the function which is at the top of this page. Then it throws the same error, but instead it is at index -1 of array of size 1.

FMath::RandRange returns a value that’s inclusive of both ends meaning you cannot pass .Num() as the maximum and directly use it as the array index. RandRange may return that value and since array indices are 0-based that index would be out of bounds.

You should probably change it to something like this

if (AttackDirection == EWeaponAttackDirection::AD_Down)
{
	// the check is optional, if you expect there to always be at least one animation you can use this otherwise you may want to add this to the if condition instead
	check(DownAnimations.Num() > 0);
	const uint8 RandomInt = FMath::RandRange(0, DownAnimations.Num() - 1);
	return DownAnimations[RandomInt];
}
// ... same for the other animations

See FMath::RandRange | Unreal Engine Documentation