Error: Accessed none trying to read property "ClosestEnemy". Node: Branch

Hey guys so I’ve ran into this error when trying to learn a code for a game I’m making. I can’t seem to track the actual problem as it really doesn’t make sense to me as to why this is occurring- I’ve set the variable but, no idea how to fix this. Will provide screenshots:
In the the first picture, this is the location where I have received my error. The second picture is actually the expansion of the node “Get Closest Enemy” that you can see after the IsValid node and before the Branch node in the first picture, and Get Closest Enemy provides the code that I’ve used to set the variable ClosestEnemy.
Thanks in advance!

Is there an enemy close enough?

I would also suggest not using a trace and instead iterating over enemies and calculating distance manually.

You could also get overlaps from a sphere component with the correct radius attached to whatever you are calling your check on.

If you notice the function to ‘get closest enemy’ returns an enemy object but you never set your variable ‘closest enemy’ to this value. So if you end up using the ‘is not valid’ path you have a null pointer to the closest enemy in the variable. If you need a visual explanation check out video #27 in the link below it discusses the accessed none error. Also noticed if you don’t have any hits your loop will simply use the ‘complete’ execution path and you won’t actually set the enemy variable in that case.