How to instantiate Physical Material and assign it to Sphere Component programmatically in C++?

I did the following:

ADodgeballProjectile::ADodgeballProjectile()
{
    PrimaryActorTick.bCanEverTick = true;
    SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere Component"));
    // others have been removed for the sake of simplicity!
    SetRootComponent(SphereComponent);

    UPhysicalMaterial* PhysicalMaterial = CreateDefaultSubobject<UPhysicalMaterial>(TEXT("Physical Material"));
    PhysicalMaterial->Friction = 0.1f;
    PhysicalMaterial->Restitution = 0.5f;
    SphereComponent->BodyInstance.SetPhysMaterialOverride(PhysicalMaterial);

}

and I got 3 errors as follows:

Error    LNK2019    unresolved external symbol "__declspec(dllimport) private: static class UClass * __cdecl UPhysicalMaterial::GetPrivateStaticClass(void)" (__imp_?GetPrivateStaticClass@UPhysicalMaterial@@CAPEAVUClass@@XZ) referenced in function "public: __cdecl ADodgeballProjectile::ADodgeballProjectile(void)" (??0ADodgeballProjectile@@QEAA@XZ)    Dodgeball    C:\Users\amd\Documents\Unreal Projects\Books\Dodgeball\Intermediate\ProjectFiles\DodgeballProjectile.cpp.obj    1    
Error    LNK1120    1 unresolved externals    Dodgeball    C:\Users\amd\Documents\Unreal Projects\Books\Dodgeball\Binaries\Win64\UE4Editor-Dodgeball-0019.dll    1    
Error    MSB3073    The command ""C:\Program Files\Epic Games\UE_4.27\Engine\Build\BatchFiles\Build.bat" DodgeballEditor Win64 Development -Project="C:\Users\amd\Documents\Unreal Projects\Books\Dodgeball\Dodgeball.uproject" -WaitMutex -FromMsBuild" exited with code 6.    Dodgeball    C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\Microsoft.MakeFile.Targets    44    

Go to %YourProjectName%.Build.cs and add "PhysicsCore" to the list of Dependency Modules.
See if it helps.

Great! It works. Thank you!

Any time you see “unresolved externals”, just go see if the new class you’re trying to use requires a dependency module. You can find it in the UE documentation. For instance, here’s the page for UPhysicalMaterial; Under References, it shows the module you need to add.