Hello all,
I hope everyone is having a great weekend so far. This question is quite specific, as I haven’t been able to find a reliable workaround—except for one, which I’m not crazy about.
Context:
I've created NPCs that each have a component variable called "EnemyHP". The idea is to link this variable with parameters and inventory interactions to create a faux combat system, where I can clone the same NPC/object multiple times in a scene. When the "EnemyHP" component variable reaches 0, a death animation plays, and the object is then removed from the scene.
I've managed to set everything up, but I’m unable to find a reliable way to continuously check the component variable changes at runtime. The main issue is that I can’t perform a "if component variable == 0" check during the interaction, as it doesn’t update in real time.
The only workaround I’ve found is to add a Trigger to every NPC, set to Trigger Type: Continuous, which checks whether the component variable is 0. If so, it plays the death animation, waits, and then removes the object from the scene. That said, I’m not a big fan of this method.
I’m also unsure if the Event Editor is the right approach here, since it may not correctly handle playing each individual prefab duplicate’s animation or removing them from the scene.
Is there anything I’ve missed, or is the trigger method my best option?
For context, I’m using version 1.82.6.
Thank you, as always, for the help!
It looks like you're new here. If you want to get involved, click one of these buttons!
Comments
The Variable / Change Component event will fire any time EnemyHP's value is altered.
Rather than calling this from the Events Editor, however, attach the Event Runner component to your NPC. When this event is attached to a GameObject, it will let you assign/select the specific Component variable it should fire for. Meaning, you can have it run an ActionList when that specific NPC's variable is changed.
Auto-creating an ActionList via the + icon will also set up a "Component variable" parameter, that you can use in a Variable: Check Action to see if it's zero or less.
Thank you, Chris — you’re a lifesaver!