If Inventory[0] breaks your game, it’s probably a nullptr. You’re calling the function NextWeapon while the inventory is empty. This is most likely a problem caused somewhere else in your code.
First, when accessing an array you should always check whether the index is actually valid. Your NextWeapon() function will fail if the array is empty (i.e. no weapon was picked up, but the player is trying to switch weapons). You can use the Inventory.IsValidIndex(…) function, or you can check whether Inventory.Num() is greater than the index you’re trying to access.
Second, since your array is holding actors, you need to make sure that the Inventory field is a UProperty. Otherwise the values added to this array will be garbage collected (meaning: destroyed), because the Engine has no way of detecting that you are still planning to use them. You can make the Inventory array a UProperty by marking it up with the UPROPERTY() macro in your class declaration for AHermesCharacter.
You should also include the actual error message in your questions. That will make it easier for us to determine what exactly is going wrong, thanks!
I know i should but that was just a test code i stared writing line by line and checking if everything work, There is no null pointer exepction index is valid and when it comes too that variable it just crashes My variable declarations are:
void AWeapon::WearWeapon(bool wear)
{
if (PickUpAnimation != NULL)
{
// Get the animation object for the arms mesh
if (AnimInstance != NULL)
{
AnimInstance->Montage_Play(PickUpAnimation, 1.f);
}
}
this->SetVisibility(wear);
}