# OnComponentHit Function not working

**URL:** <https://forums.unrealengine.com/t/oncomponenthit-function-not-working/483103>\
**Category:** World Creation\
**Tags:** oncomponenthit, unreal-engine, Physics, Collision, dynamic, question, CPP\
**Created:** [May 8, 2021, 6:25pm UTC](https://forums.unrealengine.com/t/oncomponenthit-function-not-working/483103 "2021-05-08T18:25:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![11Mdp](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@11Mdp](https://forums.unrealengine.com/u/11Mdp)\
**Post date:** [May 8, 2021, 6:25pm UTC](https://forums.unrealengine.com/t/oncomponenthit-function-not-working/483103/1 "2021-05-08T18:25:20Z")

</div>

Hello, I’m having problem with the OnComponentHit activation.

Basically I’ve written this code for my projectile class:

 ![338320-constructor.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/9/4/a/94a49d3882183d1b19fd02472430c5e1f606c3e9.png)

And this is how the OnHit function is declared and defined:

 ![338331-declaration.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/c/2/6c22dc5df05229a70efdd80e3dff276d058ce465.png)

 ![338332-definition.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/5/9/2594ed8624ae5a8e2c357317615cb079a9755945.png)

The problem is that the OnHit function is never called.

The collision settings on my projectile’s static mesh component (which is the root component and the only scene component of the actor) are the following:

 ![338333-collision-proj.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/f/4/7f4ab3b4d76bd7534e1fac8d1e2b91ac2bbc41c3.png)

And those are the collision settings of the object type “Enemy”:

 ![338334-collision-enemy.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/4/1/b4190697604aa9ae41645b7a34a681a641ef52bd.png)

In game, the only things that actually block my projectile are indeed actors with collision type of “Enemy”, but the OnHit function is never called.

Can somebody help me and tell me what I’m missing here? Thanks a lot!

---

<div class="post-metadata">

**Author:** ![Tuerer](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/tuerer/32/1008648_2.png) [@Tuerer](https://forums.unrealengine.com/u/Tuerer)\
**Post date:** [May 9, 2021, 12:28pm UTC](https://forums.unrealengine.com/t/oncomponenthit-function-not-working/483103/2 "2021-05-09T12:28:16Z")

</div>

Try binding in BeginPlay() instead of the constructor. For some reason the constructor doesn’t always bind to events correctly.

---

<div class="post-metadata">

**Author:** ![DuneGames](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/dunegames/32/538495_2.png) [@DuneGames](https://forums.unrealengine.com/u/DuneGames)\
**Post date:** [September 16, 2022, 6:22pm UTC](https://forums.unrealengine.com/t/oncomponenthit-function-not-working/483103/3 "2022-09-16T18:22:33Z")

</div>

Hey, I have the same problem with the same class. Only, binding this event in BeginPlay vs the constructor isn’t helping.

You may be asking why the similarity - it’s because it’s a tutorial on Udemy we’re following.

My code:

 ![Untitled](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/1/1/511c4a7f8661d21784f86640fe2e911f70158ccd.png)

---

<div class="post-metadata">

**Author:** ![DuneGames](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/dunegames/32/538495_2.png) [@DuneGames](https://forums.unrealengine.com/u/DuneGames)\
**Post date:** [September 16, 2022, 6:28pm UTC](https://forums.unrealengine.com/t/oncomponenthit-function-not-working/483103/4 "2022-09-16T18:28:35Z")

</div>

Never mind, found someone that had the same issue in the tutorial forums - [link here](https://www.udemy.com/course/unrealcourse/learn/lecture/31746624#questions/17599888) if you have the course to read it yourself.

The answer is though, close unreal editor, recompile (build) and then relaunch editor.

---

<div class="post-metadata">

**Author:** ![alx881](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@alx881](https://forums.unrealengine.com/u/alx881)\
**Post date:** [January 14, 2024, 9:18pm UTC](https://forums.unrealengine.com/t/oncomponenthit-function-not-working/483103/5 "2024-01-14T21:18:10Z")

</div>

Thanks man! Still having trouble figure out how Visual Code, Unreal Engine and LiveCoding vs. ‘normal’ Compiling plays together…  
ToonTanks right? So how is your Unreal Journey going so far?

---

<div class="post-metadata">

**Author:** ![DuneGames](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/dunegames/32/538495_2.png) [@DuneGames](https://forums.unrealengine.com/u/DuneGames)\
**Post date:** [January 17, 2024, 3:54am UTC](https://forums.unrealengine.com/t/oncomponenthit-function-not-working/483103/6 "2024-01-17T03:54:52Z")

</div>

A year and a half in, it does start making sense!

I don’t remember if it was ToonTanks, but it was that tutorial series on Udemy. It really helped me a lot.

You can see an update of where I’m at in building ‘the game’ here - [Edge Emperor - Game Update - Harvesting Improvements - YouTube](https://www.youtube.com/watch?v=0rOKqFYpNFw)

---

<div class="post-metadata">

**Author:** ![ajinkya\_x](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/ajinkya_x/32/1023283_2.png) [@ajinkya\_x](https://forums.unrealengine.com/u/ajinkya_x)\
**Post date:** [February 1, 2025, 3:17pm UTC](https://forums.unrealengine.com/t/oncomponenthit-function-not-working/483103/7 "2025-02-01T15:17:51Z")

</div>

add this to your header

`virtual void PostInitializeComponents() override;`

and now you could bind them like this,

```auto

void AMyMagicProjectile::PostInitializeComponents()
{
	Super::PostInitializeComponents();

	// SphereComp->OnComponentBeginOverlap.AddDynamic(this, &AMyMagicProjectile::OnSphereOverlap);
	SphereComp->OnComponentHit.AddDynamic(this, &AMyMagicProjectile::OnSphereHit);
}

```

---

<div class="post-metadata">

**Author:** ![ajinkya\_x](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/ajinkya_x/32/1023283_2.png) [@ajinkya\_x](https://forums.unrealengine.com/u/ajinkya_x)\
**Post date:** [February 2, 2025, 9:44am UTC](https://forums.unrealengine.com/t/oncomponenthit-function-not-working/483103/8 "2025-02-02T09:44:14Z")

</div>

hello, which course this ToonTanks you are referring too ?
