I'm no modder (only read a few posts) and no LUA programmer (but know some languages), and thus tried havinbg a look myself ... here is the result after a quick google search for "factorio built_entity" :
https://lua-api.factorio.com/latest/events.html shows
on_built_entity - Called when player builds something.
Contains
created_entity :: LuaEntity
player_index :: uint
stack :: LuaItemStack
on_robot_built_entity - Called when a construction robot builds an entity.
Contains
robot :: LuaEntity: The robot that did the building.
created_entity :: LuaEntity: The entity built.
stack :: LuaItemStack: The item used to do the building.
and following the link for created_entity to https://lua-api.factorio.com/0.16.0/LuaEntity.html, i see
unit_number :: uint [R] The unit number or nil if the entity doesn't have one.
in line 21-23, you do an unconditional print :
21: script.on_event({defines.events.on_built_entity, defines.events.on_robot_built_entity}, function(event)
22: local entity = event.created_entity
23: util.print(entity.name .. " placed, unit number: " .. entity.unit_number)
btw: all other print (eg lines 29 & 34) are commented out, but not line 23.
this looks pretty much like a debug command that you forgot ...
you don't check whether the ceated_entity exists or is nil, and most of all you don't check if it has a valid unit_number that is not nil. and if the unit_number is nil, we hit the crash
i have read quite often that a lot can be nil or not exist at all or be not valid, and thus a few extra checks can avoid crashes, while unnecessary checks at most cost minimal additional time to execute.
many mods call on_..._built_entity to allow other mods to do additional handling, eg after modifying something by script, after placing blueprints, etc. and when the properties of entities are copied (including modules, etc), this is probably also the case ...
ps: not directly related to this exact problem, but still an interesting read, related to modifying entities and to having no valid entities on these events: https://forums.factorio.com/viewtopic.php?t=32039