# Trying to change the material of an object on collision hit

**URL:** <https://forums.unrealengine.com/t/trying-to-change-the-material-of-an-object-on-collision-hit/451512>\
**Category:** Rendering\
**Tags:** unreal-engine, UE4, CPP, Blueprint, question\
**Created:** [June 30, 2019, 11:32pm UTC](https://forums.unrealengine.com/t/trying-to-change-the-material-of-an-object-on-collision-hit/451512 "2019-06-30T23:32:50Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Craggyy](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Craggyy](https://forums.unrealengine.com/u/Craggyy)\
**Post date:** [June 30, 2019, 11:32pm UTC](https://forums.unrealengine.com/t/trying-to-change-the-material-of-an-object-on-collision-hit/451512/1 "2019-06-30T23:32:50Z")

</div>

Hi I am trying to change the color of this floating box when it is hit by a projectile. I am not sure what the code is to change the material of an aactor in C++. This is my current code

```
// Fill out your copyright notice in the Description page of Project Settings.

#include "ProjectileTrigger.h"

// Sets default values for this component's properties
UProjectileTrigger::UProjectileTrigger()
{
	// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
	// off to improve performance if you don't need them.
	PrimaryComponentTick.bCanEverTick = true;
	BoxComp = CreateDefaultSubobject<UBoxComponent>(TEXT("RootCollision"));
	MeshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComp"));
	OnHitMesh = CreateDefaultSubobject<UMaterial>(TEXT("OnHitMaterial"));
	BoxComp->SetBoxExtent(FVector(75, 75, 50));
	BoxComp->OnComponentHit.AddDynamic(this, &UProjectileTrigger::DetectHit);
	NewMovement = FVector(50, 0, 0);
	
}

// Called when the game starts
void UProjectileTrigger::BeginPlay()
{
	Super::BeginPlay();

	// ...
	
}

// Called every frame
void UProjectileTrigger::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
	AActor* Parent = GetOwner();
	if (Parent)
	{
		Parent->SetActorLocation(Parent->GetActorLocation() + NewMovement * DeltaTime);
	}

}

void UProjectileTrigger::DetectHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
	AActor* Parent = GetOwner();
}

```

and my .h is here

```
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include <Components/BoxComponent.h>
#include "ProjectileTrigger.generated.h"

class UBoxComponent;

UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class FPSGAME_API UProjectileTrigger : public UActorComponent
{
	GENERATED_BODY()

public:	
	// Sets default values for this component's properties
	UProjectileTrigger();

protected:
	// Called when the game starts
	virtual void BeginPlay() override;

	UPROPERTY(VisibleAnywhere, Category = "CollisionComp")
	UBoxComponent* BoxComp;

	UPROPERTY(VisibleAnywhere, Category = "CollisionComp")
	UStaticMeshComponent* MeshComp;

	UPROPERTY(EditAnywhere, Category = "MaterialonHit")
	UMaterial* OnHitMesh;

	UPROPERTY(EditInstanceOnly, Category = "Movement")
	FVector NewMovement;

	UFUNCTION()
	void DetectHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);

public:	
	// Called every frame
	virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;

		
};
```
