Ue4 class define in constructor cannot be found in function below

I create a attribute class in my npc class at constructor .It is valid at begin, but when i call attribute in the function defined in npc class below, it shows attribute is not valid
ANPC::ANPC()
{
AttributeSet = CreateDefaultSubobject(“Attribute”);
if (AttributeSet)
{
UE_LOG(LogTemp, Warning, TEXT(“Attribute Cast succeeded”));
}
else
{
UE_LOG(LogTemp, Warning, TEXT(“Attribute Cast failded”));
}
then I use attribute below in this npc class
float ANPC::GetHealth()
{
if (AttributeSet)
{
UE_LOG(LogTemp, Warning, TEXT(“GetHealth success”));
return AttributeSet->GetHealth();
}
else
{
UE_LOG(LogTemp, Warning, TEXT(“GetHealth failed”));
}
return 1.0f;
}
when i run it.the output log show “Attribute Cast succeeded” and “GetHealth failed” I don’t understand how.Can somebody help me?

You didn’t set the class for the attribute:
AttributeSet = CreateDefaultSubobject<YourAttributeClassHere>(TEXT(“Attribute”));

sorry i miss that in there ,I did set in my code It works at begainning, output log show “attribute cast succeeded” at first, then it show"Get health failed"

Can somebody help me.I am stucking at this place,i search online for a long time and don’t find answers

Do you set the AttributeSet somewhere?

For instance, if you create a default subobject of class StaticMeshComponent, you need to set the static mesh later.
When you just create AttributeSet, it’s empty. You need to set an instance of the class to it.

Thanks for your help, I solve my problem.
I add this following code in my ANPC::BeginPlay()
"
if (IsValid(AbilitySystemComponent))
AttributeSet = AbilitySystemComponent->GetSet();
"
Then out log still show “Get Health failed”. Then I found I use GetHealth() in npc’s blueprint at EventBeginPlay;same place where I defined my Attributes in c++,I think it occur some priority problems.I change GetHealth() to EventEveryTick In blueprint. finally it’s working.Outlog kept showing “Get Health succeeded” .