# How to play Death Animation whenever the enemies Health is 0? (In Blueprints or C++ and spawn again with the AI Controller)

**URL:** <https://forums.unrealengine.com/t/how-to-play-death-animation-whenever-the-enemies-health-is-0-in-blueprints-or-c-and-spawn-again-with-the-ai-controller/267910>\
**Category:** Character & Animation\
**Tags:** unreal-engine, Blueprint, UE4-27, question\
**Created:** [December 23, 2021, 5:21am UTC](https://forums.unrealengine.com/t/how-to-play-death-animation-whenever-the-enemies-health-is-0-in-blueprints-or-c-and-spawn-again-with-the-ai-controller/267910 "2021-12-23T05:21:44Z")\
**Posts on this page:** 20\
**Page:** 5

<div class="post-metadata">

**Author:** ![DeadFalconking](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/deadfalconking/32/250507_2.png) [@DeadFalconking](https://forums.unrealengine.com/u/DeadFalconking)\
**Post date:** [January 7, 2022, 6:08am UTC](https://forums.unrealengine.com/t/how-to-play-death-animation-whenever-the-enemies-health-is-0-in-blueprints-or-c-and-spawn-again-with-the-ai-controller/267910/82 "2022-01-07T06:08:33Z")

</div>

I’m sorry to say this but I couldn’t get anything helpful on YouTube or any other websites on Object Wildcard. Could you please send me some references from which I can refer to?

---

<div class="post-metadata">

**Author:** ![DeadFalconking](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/deadfalconking/32/250507_2.png) [@DeadFalconking](https://forums.unrealengine.com/u/DeadFalconking)\
**Post date:** [January 8, 2022, 10:00am UTC](https://forums.unrealengine.com/t/how-to-play-death-animation-whenever-the-enemies-health-is-0-in-blueprints-or-c-and-spawn-again-with-the-ai-controller/267910/84 "2022-01-08T10:00:11Z")

</div>

@MostHost_LA @Limanima Instead of Cast To BP\_Enemy, I’m using Get All Actors Of Class and now it’s working (not spawning the enemy, but print string is working) but there’s one problem. I get this error:

> Blueprint Runtime Error: “Accessed None trying to read property Call\_Array\_Get\_Item”. Blueprint: BP\_Spawner Function: Execute Ubergraph BP\_Spawner Graph: Event Graph Node: Branch.

And I’ve changed some stuff.

> Changed the Actor Spawn What? to Actor Array Spawn What? Time. NOTE: I’ve changed the variable to Actor Array Spawn What? Time because there are 65 Enemies in the Gameplay Level (I tried attaching A Spawn What? variable back, but still it gives the same error.
> 
> ![image](https://d3kjluh73b9h9o.cloudfront.net/original/3X/7/0/70e3b6ab023a87f3c36f1965255617323f46d54d.png)

> The Tick Event Condition.
> 
> ![image](https://d3kjluh73b9h9o.cloudfront.net/original/3X/7/d/7dcb7bd64e33bef243f0c9658a5bfc1ce4325499.png)

> The BP\_Enemy has a tag name called “Enemy”. So I did this in the BeginPlay Event.
> 
> ![image](https://d3kjluh73b9h9o.cloudfront.net/original/3X/5/0/5098b905c5f139427936bbd78b47f5a03bb5eb96.png)

And when the Spawn Event is activated, the Print String function does not work. So the problem is with the **Spawn Enemy Event**.

---

<div class="post-metadata">

**Author:** ![Limanima](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/limanima/32/248560_2.png) [@Limanima](https://forums.unrealengine.com/u/Limanima)\
**Post date:** [January 9, 2022, 5:24pm UTC](https://forums.unrealengine.com/t/how-to-play-death-animation-whenever-the-enemies-health-is-0-in-blueprints-or-c-and-spawn-again-with-the-ai-controller/267910/85 "2022-01-09T17:24:45Z")

</div>

It’s very hard to help because I’ve lost the way where you’re at.  
Can you send me the project file again so I can take a look at it?  
It’s the last time I’m doing this as I don’t have much time to work on my own project…

---

<div class="post-metadata">

**Author:** ![DeadFalconking](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/deadfalconking/32/250507_2.png) [@DeadFalconking](https://forums.unrealengine.com/u/DeadFalconking)\
**Post date:** [January 10, 2022, 4:09am UTC](https://forums.unrealengine.com/t/how-to-play-death-animation-whenever-the-enemies-health-is-0-in-blueprints-or-c-and-spawn-again-with-the-ai-controller/267910/86 "2022-01-10T04:09:03Z")

</div>

You can take your own time, I don’t mean to disturb you guys with your work to solve my problem : )

Download the project files -

---

<div class="post-metadata">

**Author:** ![Limanima](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/limanima/32/248560_2.png) [@Limanima](https://forums.unrealengine.com/u/Limanima)\
**Post date:** [January 10, 2022, 4:04pm UTC](https://forums.unrealengine.com/t/how-to-play-death-animation-whenever-the-enemies-health-is-0-in-blueprints-or-c-and-spawn-again-with-the-ai-controller/267910/87 "2022-01-10T16:04:59Z")

</div>

I’m taking a look at what you have done.

1. Why are you spawning enemies in the Tick Event of BP\_Spawner?  
No need. Enemies are spawned in “BP\_Spwaner-\>Spawn Enemy”

2. In BPSpawner-\>“Aa\_Spawn\_Spots” has no elements, that’s why enemies don’t spawn

3. There are a lot of things I don’t understand  
What’s this for?  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/3X/4/3/43824f4a4894da0fea68a47b14289c953fc60c38.png)

What is “Aa Spawn Spots” supposed to hold?  
Why do you have “?” in your variable names? That’s new…  
I’m lost…

And why you are not simple spawning a BP\_Enemy?  
Here:  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/3X/9/d/9d97de412911d2ed79ab14593c4fbe7789337ab1.png)  
The class comes from here:  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/3X/1/c/1c290465111a6b16c435377aaf210fe4b2228127.png)

What’s is going on here?

By looking at your blueprints I have the feeling you don’t know exactly what you are doing and you are just copying from other examples. You have to know what you are copying. The task at hand is simple:

-Enemy got hit? Is he dead? Yes? Destroy, spawn new enemy at location x, y, z.  
While developing and testing this, create a map with a single enemy. While this is not working, just spawn the enemy at a fixed position. When you have this working, then you deal with the spawn locations.

---

<div class="post-metadata">

**Author:** ![Limanima](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/limanima/32/248560_2.png) [@Limanima](https://forums.unrealengine.com/u/Limanima)\
**Post date:** [January 10, 2022, 5:34pm UTC](https://forums.unrealengine.com/t/how-to-play-death-animation-whenever-the-enemies-health-is-0-in-blueprints-or-c-and-spawn-again-with-the-ai-controller/267910/88 "2022-01-10T17:34:05Z")

</div>

Alright, I’m spawning enemies.

The crash you’re having is due to this:

![image](https://d3kjluh73b9h9o.cloudfront.net/original/3X/b/8/b8e48c0938c21b665b04d638c18f70063905c801.png)

Move the line “DamageCollision-\>OnComponentBeginOverlap” to here:

![image](https://d3kjluh73b9h9o.cloudfront.net/original/3X/e/f/ef190a5b33ae41158829243103dcdd57b2f20dc5.png)

Then, in the BP\_Spawner in the “Spawn Enemy” event simply do this:  
(note the 300 in Z, or else enemies will spawn under the floor)  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/3X/8/4/84ce6a7d24bfe36eb609e5711d2b5e75450c4ef1.png)

Do nothing on tick:

![image](https://d3kjluh73b9h9o.cloudfront.net/original/3X/f/e/fe1d297b4de56e67f03916a8afcfdae163c32bb2.png)

When you have this working, then you deal with the spawn location.

---

<div class="post-metadata">

**Author:** ![DeadFalconking](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/deadfalconking/32/250507_2.png) [@DeadFalconking](https://forums.unrealengine.com/u/DeadFalconking)\
**Post date:** [January 11, 2022, 4:31am UTC](https://forums.unrealengine.com/t/how-to-play-death-animation-whenever-the-enemies-health-is-0-in-blueprints-or-c-and-spawn-again-with-the-ai-controller/267910/89 "2022-01-11T04:31:01Z")

</div>

And where do I call the simple Spawn Enemy Event?

---

<div class="post-metadata">

**Author:** ![DeadFalconking](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/deadfalconking/32/250507_2.png) [@DeadFalconking](https://forums.unrealengine.com/u/DeadFalconking)\
**Post date:** [January 11, 2022, 4:34am UTC](https://forums.unrealengine.com/t/how-to-play-death-animation-whenever-the-enemies-health-is-0-in-blueprints-or-c-and-spawn-again-with-the-ai-controller/267910/90 "2022-01-11T04:34:42Z")

</div>

> This is the simple Spawn Event (I’ll set the location once it’s working properly)
> 
> ![image](https://d3kjluh73b9h9o.cloudfront.net/original/3X/d/b/dbf6c3786ab2acc5a2c05843cab1d1ba3e80c5aa.png)

> This is the BeginPlay Event
> 
> ![image](https://d3kjluh73b9h9o.cloudfront.net/original/3X/9/c/9ce0d6d5b3f1ea083b14b9c22178b26ac71da8bc.png)

And I changed the lines you told me to also.

---

<div class="post-metadata">

**Author:** ![MostHost\_LA](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mosthost_la/32/907035_2.png) [@MostHost\_LA](https://forums.unrealengine.com/u/MostHost_LA)\
**Post date:** [January 11, 2022, 6:06am UTC](https://forums.unrealengine.com/t/how-to-play-death-animation-whenever-the-enemies-health-is-0-in-blueprints-or-c-and-spawn-again-with-the-ai-controller/267910/91 "2022-01-11T06:06:16Z")

</div>

That’s wrong too.

If damagecollision  
Then do the operation on it.

Otherwise you crash when it’s null.

---

<div class="post-metadata">

**Author:** ![DeadFalconking](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/deadfalconking/32/250507_2.png) [@DeadFalconking](https://forums.unrealengine.com/u/DeadFalconking)\
**Post date:** [January 11, 2022, 6:06am UTC](https://forums.unrealengine.com/t/how-to-play-death-animation-whenever-the-enemies-health-is-0-in-blueprints-or-c-and-spawn-again-with-the-ai-controller/267910/92 "2022-01-11T06:06:44Z")

</div>

So what should I do?

---

<div class="post-metadata">

**Author:** ![Limanima](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/limanima/32/248560_2.png) [@Limanima](https://forums.unrealengine.com/u/Limanima)\
**Post date:** [January 11, 2022, 11:02am UTC](https://forums.unrealengine.com/t/how-to-play-death-animation-whenever-the-enemies-health-is-0-in-blueprints-or-c-and-spawn-again-with-the-ai-controller/267910/93 "2022-01-11T11:02:12Z")

</div>

> [@DeadFalconking](#):
>
> And where do I call the simple Spawn Enemy Event?

I haven’t changed that.It’s in the BP\_Enemy when he dies.  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/3X/a/6/a6fb48a2d30d9c18c929398f316b11879c51f79e.png)

> [@post:91](#):
>
> That’s wrong too.
> 
> If damagecollision  
> Then do the operation on it.
> 
> Otherwise you crash when it’s null.

I don’t think this is needed. In some situation checking for null is not needed and I think this is one of those situations.  
If CreateDefaultSuboject fails then something is very wrong. This shouldn’t happen. This would affect the gameplay, so the game can’t continue anyway. How would the game work without the enemies collision working?  
I rather add an assert.  
You add a “if (damagecollision)” then what?  
This is irrelevant anyway for what OP is trying to achieve and that’s not because of that this isn’t working.

---

<div class="post-metadata">

**Author:** ![MostHost\_LA](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mosthost_la/32/907035_2.png) [@MostHost\_LA](https://forums.unrealengine.com/u/MostHost_LA)\
**Post date:** [January 11, 2022, 12:38pm UTC](https://forums.unrealengine.com/t/how-to-play-death-animation-whenever-the-enemies-health-is-0-in-blueprints-or-c-and-spawn-again-with-the-ai-controller/267910/94 "2022-01-11T12:38:57Z")

</div>

Whenever you instantiate during construction there is a chance other engine overrides call something before it exists (Particularly begin play).  
Either don’t call it (Which in this case is a good idea) or test that it’s valid.  
Literally just if / then

> [@Limanima](#):
>
> You add a “if (damagecollision)” then what?

Then the code that was failing.  
The if will prevent null values.  
You can also add an else.

Or you can try catch. If you want to waste time or what not…

---

<div class="post-metadata">

**Author:** ![Limanima](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/limanima/32/248560_2.png) [@Limanima](https://forums.unrealengine.com/u/Limanima)\
**Post date:** [January 11, 2022, 2:23pm UTC](https://forums.unrealengine.com/t/how-to-play-death-animation-whenever-the-enemies-health-is-0-in-blueprints-or-c-and-spawn-again-with-the-ai-controller/267910/95 "2022-01-11T14:23:57Z")

</div>

> [@post:94](#):
>
> Then the code that was failing.  
> The if will prevent null values.  
> You can also add an else.
> 
> Or you can try catch. If you want to waste time or what not…

I know you can do all that and avoid the crash. The question is, gameplay wise, what will happen? Nothing, because the game can’t continue when the actor is not correctly initialized.

Take a look at the ACharacter.cpp for example. A lot of places where null isn’t checked. Creating a mesh actor shouldn’t fail, and if it does, the game can’t continue anyway. That’s why I ratter have asserts then if !null. If !null hides bugs.

---

<div class="post-metadata">

**Author:** ![MostHost\_LA](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mosthost_la/32/907035_2.png) [@MostHost\_LA](https://forums.unrealengine.com/u/MostHost_LA)\
**Post date:** [January 11, 2022, 2:42pm UTC](https://forums.unrealengine.com/t/how-to-play-death-animation-whenever-the-enemies-health-is-0-in-blueprints-or-c-and-spawn-again-with-the-ai-controller/267910/96 "2022-01-11T14:42:18Z")

</div>

> [@Limanima](#):
>
> The question is, gameplay wise, what will happen? Nothing, because the game can’t continue when the actor is not correctly initialized.

Wrong.  
The BeginPlay event is fired several times one of these times could be invalid, and the next could be valid.  
Gameplay wise, you don’t need to stop the execution of the begin play event to ensure initialization has occurred.  
The process can happen on several threads too, so consider that.

If you are doing something else in begin play - which you usually are, since it’s initialization, using a simple if prevents crashes when the object hasn’t actually yet been properly created.  
Begin play can happed in parallel - which is another issue.

Also, don’t try and learn from acharacter.  
It’s one of the worse cpp in unreal.  
Take apart the vehicle stuff or pawn… or really just about anything else…

---

<div class="post-metadata">

**Author:** ![Limanima](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/limanima/32/248560_2.png) [@Limanima](https://forums.unrealengine.com/u/Limanima)\
**Post date:** [January 11, 2022, 2:44pm UTC](https://forums.unrealengine.com/t/how-to-play-death-animation-whenever-the-enemies-health-is-0-in-blueprints-or-c-and-spawn-again-with-the-ai-controller/267910/97 "2022-01-11T14:44:37Z")

</div>

Note that the line of code in question, moved from “beginplay” to the constructor.

---

<div class="post-metadata">

**Author:** ![MostHost\_LA](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mosthost_la/32/907035_2.png) [@MostHost\_LA](https://forums.unrealengine.com/u/MostHost_LA)\
**Post date:** [January 11, 2022, 2:46pm UTC](https://forums.unrealengine.com/t/how-to-play-death-animation-whenever-the-enemies-health-is-0-in-blueprints-or-c-and-spawn-again-with-the-ai-controller/267910/98 "2022-01-11T14:46:28Z")

</div>

I would argue that you add an If there too before initializing it to prevent crashes in odd situations where the assignment has failed.  
It’s not common, but you prevent an hard crash.

---

<div class="post-metadata">

**Author:** ![DeadFalconking](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/deadfalconking/32/250507_2.png) [@DeadFalconking](https://forums.unrealengine.com/u/DeadFalconking)\
**Post date:** [January 12, 2022, 4:35am UTC](https://forums.unrealengine.com/t/how-to-play-death-animation-whenever-the-enemies-health-is-0-in-blueprints-or-c-and-spawn-again-with-the-ai-controller/267910/99 "2022-01-12T04:35:57Z")

</div>

@Limanima So the Spawn Event is still not working.

> So there is nothing attached to BeginPlay & Tick Event. Only Spawn Event is there
> 
> ![image](https://d3kjluh73b9h9o.cloudfront.net/original/3X/8/0/802df04a25432d10218e564c8b3e2a7aab1f88b9.png)

> After the BP\_Enemy is Destroyed I call the Spawn Event there.
> 
> ![image](https://d3kjluh73b9h9o.cloudfront.net/original/3X/7/b/7b5937072d6d485f6f9b70b85e209115197c4128.png)

---

<div class="post-metadata">

**Author:** ![Limanima](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/limanima/32/248560_2.png) [@Limanima](https://forums.unrealengine.com/u/Limanima)\
**Post date:** [January 12, 2022, 1:44pm UTC](https://forums.unrealengine.com/t/how-to-play-death-animation-whenever-the-enemies-health-is-0-in-blueprints-or-c-and-spawn-again-with-the-ai-controller/267910/100 "2022-01-12T13:44:23Z")

</div>

Remember I’ve told you to set z location to 300 or else enemies would spawn bellow ground?

Edit: never mind, you are setting to 300…

Place a breakpoint in “Spawn-\>SpawnActor BP Enemy” and I see if the execution breaks there.

---

<div class="post-metadata">

**Author:** ![DeadFalconking](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/deadfalconking/32/250507_2.png) [@DeadFalconking](https://forums.unrealengine.com/u/DeadFalconking)\
**Post date:** [January 12, 2022, 2:08pm UTC](https://forums.unrealengine.com/t/how-to-play-death-animation-whenever-the-enemies-health-is-0-in-blueprints-or-c-and-spawn-again-with-the-ai-controller/267910/101 "2022-01-12T14:08:35Z")

</div>

Breakpoint? What do you mean by that?  
Edit: Okay sorry I got what you mean. Should I set the breakpoint on Spawn Event Node, Spawn Actor From Class or both?

---

<div class="post-metadata">

**Author:** ![Limanima](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/limanima/32/248560_2.png) [@Limanima](https://forums.unrealengine.com/u/Limanima)\
**Post date:** [January 12, 2022, 2:43pm UTC](https://forums.unrealengine.com/t/how-to-play-death-animation-whenever-the-enemies-health-is-0-in-blueprints-or-c-and-spawn-again-with-the-ai-controller/267910/102 "2022-01-12T14:43:23Z")

</div>

Here.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/3X/f/3/f336483796e3bd45699423b09528cc7d84365acb.png)  
Does the execution stops here?

[Previous page](https://forums.unrealengine.com/t/how-to-play-death-animation-whenever-the-enemies-health-is-0-in-blueprints-or-c-and-spawn-again-with-the-ai-controller/267910.md?page=4)

[Next page](https://forums.unrealengine.com/t/how-to-play-death-animation-whenever-the-enemies-health-is-0-in-blueprints-or-c-and-spawn-again-with-the-ai-controller/267910.md?page=6)
