Decrementing an integer does not work for multiple characters

Hello all. In my game, that I have made from the first-person template, I have 5 characters. When a character is shot, it is destroyed and a sound effect plays. My aim is to make it so that when all 5 characters are destroyed, a new level is opened.

I have created an integer variable VirusCount with a default value of 5. Each time a character is destroyed, it is supposed to decrement by 1. If it becomes less than 1, the new level should open.

However, the variable does not decrement, I have tested this.

This is what I currently have in the character blueprint:

I made this blueprint and then dragged the characters into the game level. It created 5 different characters:
image
Maybe this has something to do with it, that they are seperate?

Any assistance would be appreciated on what is currently wrong and how to achieve my goal.

You need to count that in some other place than character/actor that is destroyed.
For eg player controller, player pawn game instance or game mode.

And just in case:

  • code (and actor) you are editing in blueprints is not code/place that runs it during runtime of game
  • you edit template of actor, unreal editor kind of lies and pretends it is the code (or hides that you editing code that is not what actually runs). It is fine and speeds things, but you should be aware of that little trick.
  • so your variable in editor is same as FIRST actor you run (this is what editor fakes), but every other actor from same template (CLASS) has its own copy of that variable. As result you have as many “Virus Count” variables as there are characters.

So for your code to properly work you need that “Virus Count” variable in actor that has only single copy of it running. Best for it is game mode (for singleplayer games).

Move your variable there, get some tutorial about blueprints communication.

ps.
Do not use level blueprint for code, it is bad idea.

Thanks for the advice!