Skeletal Mesh Components Return Null PTR (Breakpoint Usage, Null Variables, Possibly Setting Variables incorrectly)

The Summary

The issue is that when I try to start the engine using MSVS’s Local Windows Debugger, MSVS triggers a breakpoint and tells me that the Skeletal Meshes are NULL PTR for some reason. I don’t know why. If it is because I am setting the variables incorrectly? Please tell me how to do it properly.

I compile and look in the editor for the proper changes and everything is fine for some reason, but when I try to open the editor using MSVS it triggers a breakpoint saying the skeletal meshes are null. Please any advice would be much appreciated thank you for your time and patience. If I am doing everything correctly then it might be a bug…I don’t know.

The . H

//Here is the .h File where the variables are.
            AMainCharacter();
            UPROPERTY(EditAnywhere, Category = "EnemyWeaponBaseMesh")
            class USkeletalMeshComponent* EnemyWeaponBaseMesh;
        UPROPERTY(EditAnywhere, Category = "SkeletonRightArm ")
            class USkeletalMeshComponent* SkeletonRightArm;

The .CPP Constructor

    AMainCharacter::AMainCharacter()
    {   
        RootComponent = GetCapsuleComponent();
        GetMesh()->SetupAttachment(RootComponent);
        /*This is where the NULL PTR error starts. The debugger says that 
        EnemyWeaponBase Mesh Returns NULL.*/
        EnemyWeaponBaseMesh = CreateDefaultSubobject<USkeletalMeshComponent> 
        (TEXT("EnemyWeaponBase"));
         EnemyWeaponBaseMesh->AttachToComponent(SkeletonRightArm, 
         FAttachmentTransformRules::SnapToTargetIncludingScale, 
         FName("EnemyWeaponBase"));
        SkeletonRightArm = CreateDefaultSubobject<USkeletalMeshComponent> 
        (TEXT("RightArmSkeleton"));
        SkeletonRightArm->AttachToComponent(GetMesh(), 
        FAttachmentTransformRules::SnapToTargetIncludingScale, 
          FName("RightShoulderSocket"));     
    }

Answer

Firstly, ACharacter is an Engine class, you should not be modifying it, instead you should have a subclass of this and modify it instead. If you already have subclassed it, you do not need to setup the attachments for the CapulseComponent as the RootComponent. It will already be the RootComponent, the same goes for the Mesh.

Secondly, SetupAttachment() should be used instead of AttachToComponent() within constructors.

Make sure to always check your pointers validity before using them.

Leave a Reply

Your email address will not be published. Required fields are marked *