# How can I detect/count number of objects spawned in a room?

**URL:** <https://forums.unrealengine.com/t/how-can-i-detect-count-number-of-objects-spawned-in-a-room/458346>\
**Category:** Blueprint\
**Tags:** object-picker, spawn-rate, unreal-engine, Blueprint, question\
**Created:** [November 12, 2019, 11:29pm UTC](https://forums.unrealengine.com/t/how-can-i-detect-count-number-of-objects-spawned-in-a-room/458346 "2019-11-12T23:29:44Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Deygus](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/deygus/32/982675_2.png) [@Deygus](https://forums.unrealengine.com/u/Deygus)\
**Post date:** [November 12, 2019, 11:29pm UTC](https://forums.unrealengine.com/t/how-can-i-detect-count-number-of-objects-spawned-in-a-room/458346/1 "2019-11-12T23:29:44Z")

</div>

So I built an Item Spawner and it’s set to spawn items every so often…

What I want to do is detect how many of those items exist in a room/area and if its too many its game over/player killed.

I’m thinking maybe a bounds volume or something?

Can someone help me figure out how to do this or point me in the right direction? I’ve been mucking around with it all day and so far not much luck…

The closest idea I have at the moment is maybe somehow counting the objects as they spawn and if it reaches a certain set limit then I could do game over potentially?

---

<div class="post-metadata">

**Author:** ![Koraemon](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Koraemon](https://forums.unrealengine.com/u/Koraemon)\
**Post date:** [November 13, 2019, 1:50am UTC](https://forums.unrealengine.com/t/how-can-i-detect-count-number-of-objects-spawned-in-a-room/458346/2 "2019-11-13T01:50:46Z")

</div>

I would make a new trigger box class and when I need to check how many items are in the volume I would check the overlapping actors.

```
TArray<AActor* > ItemsInArea;
    
BoxComponent->GetOverlappingActors(ItemsInArea);

return ItemsInArea.Num();

```

Then check the size of the array. You might want to replace AActor\* with your item base class. If the objects don’t despawn and not pickup-able, you could count the objects as they spawn.

---

<div class="post-metadata">

**Author:** ![Ruffhaus](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/ruffhaus/32/1194663_2.png) [@Ruffhaus](https://forums.unrealengine.com/u/Ruffhaus)\
**Post date:** [November 13, 2019, 5:30am UTC](https://forums.unrealengine.com/t/how-can-i-detect-count-number-of-objects-spawned-in-a-room/458346/3 "2019-11-13T05:30:11Z")

</div>

I would do the way you are thinking, and count them against the limit on spawn. I’m assuming you are spawning them using SpawnActorFromClass? If so there is a return node you can add that instance that is spawned to an array and check whether it has passed the maximum allowed and if it has then run your logic for the fail state.

 ![292255-countobjects.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/1/6/516ddbb8674dc87a30f795cd82038c5fd4de3ed0.png)

If you are destroying the spawning objects you can then remove them by their reference from the array as well.

This will give you all the objects spawned by that spawner, but across the whole world not based on their location. If you want to check them in a specific area, then Koraemon’s answer is the way to go. The blueprint script for that look like this.

 ![292256-overlapping.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/7/8/b785451d4695ddf8caa296c324fc369c2ce4c15f.png)

---

<div class="post-metadata">

**Author:** ![CrispClover](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/crispclover/32/1140157_2.png) [@CrispClover](https://forums.unrealengine.com/u/CrispClover)\
**Post date:** [November 13, 2019, 5:30am UTC](https://forums.unrealengine.com/t/how-can-i-detect-count-number-of-objects-spawned-in-a-room/458346/4 "2019-11-13T05:30:47Z")

</div>

I would build it like a scoring system. Have an integer in your GameMode, increment on spawn, decrement when one of the actors is destroyed. Check the number of actors after each increment. If it’s too many, end the game.

For extra style points you could instead write a subsystem for this. [Programming Subsystems Livestream](https://www.youtube.com/watch?v=v5b1FvKBYzc)

---

<div class="post-metadata">

**Author:** ![BlueMind\_Studio](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@BlueMind\_Studio](https://forums.unrealengine.com/u/BlueMind_Studio)\
**Post date:** [November 13, 2019, 5:32am UTC](https://forums.unrealengine.com/t/how-can-i-detect-count-number-of-objects-spawned-in-a-room/458346/5 "2019-11-13T05:32:28Z")

</div>

If you want to know how much spawned in generell usethe getAllActorsOfClass Node and Count the Array. If you want it in a rooms, use a boxTrace (or multiple with different Locations so you can simulate Like hallways etc)

---

<div class="post-metadata">

**Author:** ![CrispClover](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/crispclover/32/1140157_2.png) [@CrispClover](https://forums.unrealengine.com/u/CrispClover)\
**Post date:** [November 13, 2019, 5:35am UTC](https://forums.unrealengine.com/t/how-can-i-detect-count-number-of-objects-spawned-in-a-room/458346/6 "2019-11-13T05:35:01Z")

</div>

Just a side note: GetAllActorsOfClass is slow. It works though.

---

<div class="post-metadata">

**Author:** ![BlueMind\_Studio](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@BlueMind\_Studio](https://forums.unrealengine.com/u/BlueMind_Studio)\
**Post date:** [November 13, 2019, 5:45am UTC](https://forums.unrealengine.com/t/how-can-i-detect-count-number-of-objects-spawned-in-a-room/458346/7 "2019-11-13T05:45:35Z")

</div>

You May Not want it in Tick. But when I understand the OP He wants it in several situations and thats fine to use it. One can also use EQS but I doubt it is faster.

---

<div class="post-metadata">

**Author:** ![Deygus](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/deygus/32/982675_2.png) [@Deygus](https://forums.unrealengine.com/u/Deygus)\
**Post date:** [November 13, 2019, 2:31pm UTC](https://forums.unrealengine.com/t/how-can-i-detect-count-number-of-objects-spawned-in-a-room/458346/8 "2019-11-13T14:31:06Z")

</div>

Thank you for the help and suggestion

---

<div class="post-metadata">

**Author:** ![Deygus](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/deygus/32/982675_2.png) [@Deygus](https://forums.unrealengine.com/u/Deygus)\
**Post date:** [November 13, 2019, 2:31pm UTC](https://forums.unrealengine.com/t/how-can-i-detect-count-number-of-objects-spawned-in-a-room/458346/9 "2019-11-13T14:31:57Z")

</div>

I think I kinda understand but I struggle with the coding a lot however, i think i can translate that into blueprints similarly, thank you very much

---

<div class="post-metadata">

**Author:** ![Deygus](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/deygus/32/982675_2.png) [@Deygus](https://forums.unrealengine.com/u/Deygus)\
**Post date:** [November 13, 2019, 2:38pm UTC](https://forums.unrealengine.com/t/how-can-i-detect-count-number-of-objects-spawned-in-a-room/458346/10 "2019-11-13T14:38:10Z")

</div>

@Ruffhaus Games

Thank you so much for going the extra mile to provide me with the Blueprint Script Example. I’m a visual learner so I think that probably helps me the most right now to give me a better idea on how to proceed with it. I’m trying to work on building my first Android Game with a simple fun idea I had. I’ll be sure to mention all of you guys in the game credits as an extra thanks for all your help.Anyhow, I’ll give this a try later on today after work.

I keep fiddling around with this stuff and watching lots of youtube videos trying to figure things out and understand it all but still struggling to understand more of exactly what I am doing other than recalling it from memory after using it a lot.

Actually, I’m using a getAllActorsFromClass although I could probably do just one but I have yet to decide if I want one object or three so I might change it to just one.

---

<div class="post-metadata">

**Author:** ![Deygus](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/deygus/32/982675_2.png) [@Deygus](https://forums.unrealengine.com/u/Deygus)\
**Post date:** [November 13, 2019, 2:40pm UTC](https://forums.unrealengine.com/t/how-can-i-detect-count-number-of-objects-spawned-in-a-room/458346/11 "2019-11-13T14:40:34Z")

</div>

Yeah thanks for confirming that, I figured out another way using the BeginPlay Node. 😃

---

<div class="post-metadata">

**Author:** ![Deygus](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/deygus/32/982675_2.png) [@Deygus](https://forums.unrealengine.com/u/Deygus)\
**Post date:** [November 13, 2019, 2:43pm UTC](https://forums.unrealengine.com/t/how-can-i-detect-count-number-of-objects-spawned-in-a-room/458346/12 "2019-11-13T14:43:22Z")

</div>

That’s what I’m using now because I am unsure of if I want to spawn one object or multiple but I think I might change it to just spawning one otherwise it would probably be three objects maybe.

If I can figure out a way to just easily change the color material on it at random then I’ll probably just stick to one object.

---

<div class="post-metadata">

**Author:** ![Deygus](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/deygus/32/982675_2.png) [@Deygus](https://forums.unrealengine.com/u/Deygus)\
**Post date:** [November 14, 2019, 2:48am UTC](https://forums.unrealengine.com/t/how-can-i-detect-count-number-of-objects-spawned-in-a-room/458346/13 "2019-11-14T02:48:25Z")

</div>

@Ruffhaus Games

So I just created the Object Counter but it’s counting 3 at a time as 1? I think this is because I have 3 spawn points spawning 3 different objects. I decided to go with 3 different types of meshes for variance just so the game would look more interesting.

How can I correct that problem?

Also, I couldn’t seem to get the box collision to work after making the same example so I think I did something wrong or something is wrong somewhere. Is it working on your end?

---

<div class="post-metadata">

**Author:** ![Ruffhaus](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/ruffhaus/32/1194663_2.png) [@Ruffhaus](https://forums.unrealengine.com/u/Ruffhaus)\
**Post date:** [November 16, 2019, 4:25am UTC](https://forums.unrealengine.com/t/how-can-i-detect-count-number-of-objects-spawned-in-a-room/458346/14 "2019-11-16T04:25:54Z")

</div>

So if you are doing multiple spawn points but want them all to contribute to an overall number of spawned enemies, I would have the array of spawned enemies and all subsequent logic stored somewhere else rather than the spawner itself, probably the game mode or game state, then do all the checking there. Just let the spawn points handle the actual spawning logic.

 ![292444-consolidated.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/d/a/ada556a7b294cbfe54147dd7fbafbfc17ab189e4.png)

In terms of the overlapping actors/box collision, it might be to do with the collision profiles? It is hard to diagnose without seeing it.

---

<div class="post-metadata">

**Author:** ![Deygus](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/deygus/32/982675_2.png) [@Deygus](https://forums.unrealengine.com/u/Deygus)\
**Post date:** [November 17, 2019, 4:31am UTC](https://forums.unrealengine.com/t/how-can-i-detect-count-number-of-objects-spawned-in-a-room/458346/15 "2019-11-17T04:31:42Z")

</div>

Okay, yeah that’s kinda where I’m at now, getting really close now.

I have a Printstring that is showing the boxes being counted but its counting 3 at a time strangely

---

<div class="post-metadata">

**Author:** ![Deygus](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/deygus/32/982675_2.png) [@Deygus](https://forums.unrealengine.com/u/Deygus)\
**Post date:** [November 17, 2019, 3:30pm UTC](https://forums.unrealengine.com/t/how-can-i-detect-count-number-of-objects-spawned-in-a-room/458346/16 "2019-11-17T15:30:47Z")

</div>

I finally got the Item Counter working after mucking around with it for about 2 hours this morning! Yay!! Thanks so much for all your help!!!
