Crash trying to set FP_Gun

I want to allow the player to equip multiple types of tools within my game.

In the character.h I have a tool, which is a blueprint extended class, and i’ve selected that class in the editor

UPROPERTY(EditAnywhere)
TSubclassOf<ATool> Tool;

In my constructor, this line makes my game crash:

	// Create a gun mesh component
FP_Gun = Cast<ATool>(Tool)->GetMesh();

And GetMesh() is a function that returns a USkeletalMesh from my Tool class, here is the class:

FORCEINLINE USkeletalMeshComponent* GetMesh() const { return WieldableMesh; }

private:

UPROPERTY(EditDefaultsOnly)
USkeletalMeshComponent* WieldableMesh;

Does anyone know why attempting to call GetMesh crashes my game? Am I approaching this problem incorrectly?

You never set the Tool pointer to point to an actual instance of the class ATool, thus the pointer is null. Use Constructor Initialization properties in order to spawn your ATool and then proceed to set the mesh.

Thank you :slight_smile: Is there any guidance you could give me on how to do that?

Inside of the tool constructor, I call the CreateDefaultSubobject function to initialize the mesh - I suppose thats not what you mean?

Initialize the actor and then grab the mesh reference. You are not directly spawning a MeshComponent that way, but a seperate actor that you grab his StaticMesh component.