TSubObjectPtr not working

Hi, I know this is probable a stupid question but I’m new to programming, and I’m working out a book, Learning C++ by Creating Games with UE4. I’m pretty sure I have entered all the code correctly, but it won’t work. My code looks like this:

#pragma once

#include "GameFramework/Character.h"
#include "NPC.generated.h"

UCLASS()

class BASICCPP_API ANPC : public ACharacter
{
	GENERATED_UCLASS_BODY()

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Collision)
	TSubobjectPtr<class USphereComponent> ProxSphere;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = NPCMessage)
		FString NpcMessage;
	
public:
	// Sets default values for this character's properties
	ANPC();

	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

	// Called every frame
	virtual void Tick(float DeltaSeconds) override;

	// Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;
};

Any help would be much appreciated! Thanks!

Are you able to provide the error message?

hi im fairly new to unreal. but i think i read that
TSubObjectPtr are depricated and that u should jsut use raw pointers like AActor* and stuff.

I am not sure what u try to accomplish, but maybe u need something like that.
UPROPERTY(EditDefaultsOnly, Category=Damage)
TSubclassOf DamageType;

Hey there, if you are trying to create a sphere component that will belong to your object then you should just do:

.h

UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = "Test")
USphereComponent * Collision;

.cpp on the constructor

Collision = CreateDefaultSubobject<USphereComponent>("Collision");
Collision->InitSphereRadius(32.0f);
Collision->SetupAttachment(Mesh);