How to make an object die after a set amount of time in Unreal Engine

- by

With spawned objects like bullets and projectiles, there’s a danger of creating a memory leak in our projects. This can happen when things are instantiated and added to the game world, but are never removed. Imagine creating a plethora of flying bullets that are only needed for a moment, but when not removed, they’ll keep existing (and flying) forever. Eventually this will lead to a memory overload, or at least severe performance issues.

While we can implement methods that take care of such objects manually, Unreal Engine has an easy way of assigning an Initial Lifespan to each object. If said lifespan is exceeded, the object is automatically destroyed. We can do this in the Blueprint Class Defaults, under Actor.

The value here is given in seconds, so this object will live for exactly 2 seconds after instantiation. No further clean-up is necessary.

If you enjoy my content, please consider supporting me on Ko-fi. In return you can browse this whole site without any pesky ads! More details here.

Leave a Comment