I have the following code:
APracticeProjectCharacter::APracticeProjectCharacter(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
static ConstructorHelpers::FObjectFinder<UMaterialInstance> materialAsset(TEXT("Material'/Game/ThirdPerson/Materials/playerMaterial_Inst.playerMaterial_Inst'"));
if (materialAsset.Succeeded())
material = (UMaterialInstanceDynamic*)materialAsset.Object;
}
void APracticeProjectCharacter::Tick(float deltaTime)
{
Super::Tick(deltaTime);
material->SetScalarParameterValue(TEXT("power"), maxPowerLevel / powerLevel);
}
I am trying to load material from the assets and change some parameters. On the constructor, material is not null and if I ser paramater value there it works. but on Tick function material is null and it leads to run time error. I understand why this is happaning. material is a UMaterialInstanceDynamic pointer and when FObjectFinder.object dies, materials dies too. but why does it goes null? it is static.
I also have tried to change the contructor for the following:
static ConstructorHelpers::FObjectFinder<UMaterialInstance> materialAsset(TEXT("Material'/Game/ThirdPerson/Materials/playerMaterial_Inst.playerMaterial_Inst'"));
if (materialAsset.Succeeded())
material = UMaterialInstanceDynamic::Create(materialAsset.Object, this);
But chainging the paramater is not working. Even if I change it from the constructor.
How can I get this work?