You're loading the key of the last replaced unit which an entirely different unit than the picked unit.
The Replace action removes the original unit and creates a new unit in it's place.
What you need to do is Load the key of the picked unit into an Integer variable, then replace the unit, and finally check the loaded value:
-
Set Variable LoadedInt = (Load 0 of (Key (Picked unit)) from FactoryProduction
-
Unit - Replace (Picked unit) with a Factory (Atomic) using The old unit's life and mana
-
If (LoadedInt Equal to 1) then Order (Last replaced unit) to Undead Crypt Lord - Activate Carrion Beetles
You may also want to clear the Child hashtables of your Picked unit right after setting LoadedInt.
-
Set Variable LoadedInt = (Load 0 of (Key (Picked unit)) from FactoryProduction
-
Hashtable - Clear all child hashtables of (Key(Picked unit)) from FactoryProduction
This will clear the data (which is no longer needed) and help keep things running efficiently.
Last thing, this leaks a Unit Group:
-
Unit Group - Pick every unit in (Units owned by (Owner of (Triggering unit)) matching ((Unit-type of (Matching unit)) Equal to Factory (Industrial))) and do (Actions)