TSubclassOf set to NULL

//--------Shooter.h--------------

public:

	UPROPERTY(EditDefaultsOnly, Category = Equipment)
		TSubclassOf<AGun> WeaponSubclass;

//----------------Shooter.cpp (constructor)-----------------------------------------

	UWorld* World = GetWorld();
	if (World)
	{
		if (WeaponSubclass)
		{
			FActorSpawnParameters SpawnParams;
			Weapon = World->SpawnActor<AGun>(WeaponSubclass, SpawnParams);
			if (Weapon)
			{
				Weapon->AttachRootComponentTo(GetMesh(), WeaponSocketName, EAttachLocation::SnapToTarget);
			}
		}
	}

I set “WeaponSubclass” in the editor to an AGun blueprint, but whenever I run the game, WeaponSubclass is null.

Blueprints will set your properties after the constructor is called:

  1. Constructor
  2. OnConstructed
  3. PostActorConstruction

I think if you move the initialization to OnConstructed then it will not be null.

Thanks a lot.