How to make a function to change material?

How to make function to change certain actor material ? I have actor class with main code like this:

ADresserBuilding::ADresserBuilding()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	Mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Dresser_building"));
	RootComponent = Mesh;

	static ConstructorHelpers::FObjectFinder<UStaticMesh> MeshToUse(TEXT("StaticMesh'/Game/Models/Dresser.Dresser'")); 
	UStaticMesh* Asset = MeshToUse.Object;
	Mesh->SetStaticMesh(Asset);

	SetOpacityMaterial();
}

void ADresserBuilding::SetNormalMaterial()
{
	static ConstructorHelpers::FObjectFinder<UMaterial> NormalMaterial(TEXT("Material'/Game/Models/eb_dresser_01.eb_dresser_01'"));
	Mesh->SetMaterial(0, NormalMaterial.Object);
} 


void ADresserBuilding::SetOpacityMaterial()
{
	static ConstructorHelpers::FObjectFinder<UMaterial> OpacityMaterial(TEXT("Material'/Game/Models/eb_dresser_01.eb_dresser_01'"));
	Mesh->SetMaterial(0, OpacityMaterial.Object);
}

And next one that tries to call function but it crush editor when I try like this.

AStrategy_PlayerController::AStrategy_PlayerController()
{
	bShowMouseCursor = true;
	DefaultMouseCursor = EMouseCursor::Crosshairs;
}


void AStrategy_PlayerController::BeginPlay()
{
	Super::BeginPlay();

	bActivateDresser = false;
	
	HUDPtr = Cast<AStrategy_HUD>(GetHUD());

	SpawnedActorsNumber = 0;
} 

void AStrategy_PlayerController::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	
	if (bActivateDresser == true)
	{
		SpawnedActors[SpawnedActorsNumber]->SetActorLocation(GetMousePos(), false, 0, ETeleportType::None);
	}
}


void AStrategy_PlayerController::SetupInputComponent()
{
	Super::SetupInputComponent();

	InputComponent->BindAction("LeftMouseClick", IE_Released, this, &AStrategy_PlayerController::SetBuilding);
	InputComponent->BindAction("ActivateDres", IE_Pressed, this, &AStrategy_PlayerController::ActivateDresser);
} 

void AStrategy_PlayerController::SetBuilding()
{
	if (bActivateDresser == true)
	{
		SpawnedActors[SpawnedActorsNumber]->SetActorLocation(GetMousePos(), false, 0, ETeleportType::None);
		SpawnedActors[SpawnedActorsNumber]->SetNormalMaterial(); **//this one cause problem**
   		SpawnedActorsNumber++;
		bActivateDresser = false;
	}
}

void AStrategy_PlayerController::ActivateDresser()
{
	bActivateDresser = true;
	FVector Location(0.0f, 0.0f, 0.0f);
	FRotator Rotation(0.0f, 0.0f, 0.0f);
	ADresserBuilding* NewActor = GetWorld()->SpawnActor<ADresserBuilding>(GetMousePos(), Rotation);
	SpawnedActors.Add(NewActor);

}

FVector AStrategy_PlayerController::GetMousePos()
{
	FHitResult Hit;
	GetHitResultUnderCursor(ECollisionChannel::ECC_Visibility, false, Hit);

	return Hit.Location;
}

Thank you for your response.

Your crash is probably being caused by a null pointer (nullptr) exception, or perhaps by the use of the ConstructorHelpers library outside of a class constructor.

In general, to avoid null pointer exceptions, you want to make sure your pointers are ‘valid’ before attempting to de-reference them with the -> operator. Even a simple if (MyObjectPointer) would do the trick, though you’d have to handle what to do in case the pointer is actually not pointing to anything.

On the subject of loading project assets during runtime, I recommend you look at this article from the official docs: Referencing Assets. It shows some examples of the correct ways of doing so in C++.

Finally, have you considered setting references to your materials and meshes in the editor, using a blueprint derived from your ADresserBuilding class? In other words, if you make some UPROPERTY variables on your ADresserBuilding class for the materials and meshes you need, you’ll be able to view and set these variables from within the editor. This way, you won’t need to use hard asset references in C++, and it also allows you (or other artists) to change out these assets without having to recompile the code. Just a suggestion.

Thank you for ur answer. I already found the problem: FObjectFinder should be used inside actors constructor because ""FObjectFinders can't be used outside of constructors". Im sure that pointers okey, but thank you one more time, Ill add check before use it. I made all UPROPERTY variables to use it with blueprint. However Im studying c++ and wanna see how it works if to make project by code only where it`s possible to create plugin in future.