# Can't get child class instance from "OnHit" function

**URL:** <https://forums.unrealengine.com/t/cant-get-child-class-instance-from-onhit-function/448228>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [May 2, 2019, 8:16am UTC](https://forums.unrealengine.com/t/cant-get-child-class-instance-from-onhit-function/448228 "2019-05-02T08:16:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![micklo717](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@micklo717](https://forums.unrealengine.com/u/micklo717)\
**Post date:** [May 2, 2019, 8:16am UTC](https://forums.unrealengine.com/t/cant-get-child-class-instance-from-onhit-function/448228/1 "2019-05-02T08:16:38Z")

</div>

Hello!  
I’m trying to make a function which changes action by hit object and use “OnHit” function.  
Some objects are made from [Y class] inherited “AActor” class.  
“OnHit” function has “AActor\* OtherActor” arugement, but I can’t cast “AActor\*” to [Y class]\*.  
So I can’t get information of hit object.

How should I deal with it?

Thanks for reading and sorry for my poor English!

---

<div class="post-metadata">

**Author:** ![Lardo\_Deepdelver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Lardo\_Deepdelver](https://forums.unrealengine.com/u/Lardo_Deepdelver)\
**Post date:** [May 2, 2019, 9:07am UTC](https://forums.unrealengine.com/t/cant-get-child-class-instance-from-onhit-function/448228/2 "2019-05-02T09:07:47Z")

</div>

if your hit object is a Y class or inherits from a Y class you should be totally able to cast to it?

You created a new actor right?  
something like

```
class AYClass : public AActor  

```

and the object you are hitting is of that type?  
and you use

```
auto* HitActorAsYClass = Cast<AYClass>(OtherActor);
if (HitActorAsYClass)
  HitActorAsYClass->DoSomething();
```

---

<div class="post-metadata">

**Author:** ![micklo717](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@micklo717](https://forums.unrealengine.com/u/micklo717)\
**Post date:** [May 2, 2019, 9:45am UTC](https://forums.unrealengine.com/t/cant-get-child-class-instance-from-onhit-function/448228/3 "2019-05-02T09:45:02Z")

</div>

Thank you for advice!

Hit object have various class. So it’s class type can be not only [Y class] but also [A class] or [B class] …etc.

It may be fine by a following way

```
auto* HitActorAsYClass = Cast<AYClass>(OtherActor);
if (HitActorAsYClass) {
   HitActorAsYClass->DoSomething();
} else {
   auto* HitActorAsAClass = Cast<AAClass>(OtherActor);
   if (HitActorAsAClass) {
      HitActorAsAClass->DoSomething();
   } else {
      auto* HitActorAsBClass = Cast<ABClass>(OtherActor);
      if (HitActorAsBClass) {
         HitActorAsBClass->DoSomething();
      } else {
         // if hit actor class is C, D, E...
      }
   } 
}

```

But it would become nested branch.  
Ideally I want to do like this (it has error)

```
auto* HitActor = Cast<OtherActor->GetClass()>(OtherActor);
HitActor->DoSomething();
```

---

<div class="post-metadata">

**Author:** ![Lardo\_Deepdelver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Lardo\_Deepdelver](https://forums.unrealengine.com/u/Lardo_Deepdelver)\
**Post date:** [May 2, 2019, 10:03am UTC](https://forums.unrealengine.com/t/cant-get-child-class-instance-from-onhit-function/448228/4 "2019-05-02T10:03:18Z")

</div>

casting can not work with runtime classes, it needs to know what to cast to at compile time

sounds like what you want to do is use an interface

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — ue4community.wiki! You will be able to find content from the official...

basically all your classes that should DoSomething need to implement that interface and in the hit you check if the object implements it, and if so call it

---

<div class="post-metadata">

**Author:** ![micklo717](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@micklo717](https://forums.unrealengine.com/u/micklo717)\
**Post date:** [May 2, 2019, 10:07am UTC](https://forums.unrealengine.com/t/cant-get-child-class-instance-from-onhit-function/448228/5 "2019-05-02T10:07:38Z")

</div>

Thank you for quick replay!  
I will learn it !

---

<div class="post-metadata">

**Author:** ![Lardo\_Deepdelver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Lardo\_Deepdelver](https://forums.unrealengine.com/u/Lardo_Deepdelver)\
**Post date:** [May 2, 2019, 10:13am UTC](https://forums.unrealengine.com/t/cant-get-child-class-instance-from-onhit-function/448228/6 "2019-05-02T10:13:19Z")

</div>

if you want I can write a small example,  
just takes a little bit more time then a fast answer 😛

---

<div class="post-metadata">

**Author:** ![micklo717](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@micklo717](https://forums.unrealengine.com/u/micklo717)\
**Post date:** [May 2, 2019, 10:41am UTC](https://forums.unrealengine.com/t/cant-get-child-class-instance-from-onhit-function/448228/7 "2019-05-02T10:41:14Z")

</div>

I have to cast [OtherActor] to InterfaceClass and then, check it can be used by “not NULL”.

I think I can implement now!  
Thank you ! I spent more than 10 hours around this problem! Thank you !
