This is one way to detect the closest unit of some sort
-
Actions
-

Set VariableSet Point_Temp = (Position of (Triggering unit))
-

Set VariableSet UnitGroup_Temp = (Units within 5000.00 of Point_Temp matching (((Matching unit) belongs to an enemy of (Owner of (Triggering unit)).) Equal to True).)
-

Set VariableSet Real_Distance = 5000.00
-

Unit Group - Pick every unit in UnitGroup_Temp and do (Actions)
-


Loop - Actions
-



Set VariableSet Point_Temp2 = (Position of (Picked unit))
-



If (All Conditions are True) then do (Then Actions) else do (Else Actions)
-




If - Conditions
-





(Distance between Point_Temp and Point_Temp2) Less than or equal to Real_Distance
-




Then - Actions
-





Set VariableSet Real_Distance = (Distance between Point_Temp and Point_Temp2)
-





Set VariableSet Unit_Target = (Picked unit)
-




Else - Actions
-



Custom script: call RemoveLocation(udg_Point_Temp2)
-

Custom script: call RemoveLocation(udg_Point_Temp)
-

Custom script: call DestroyGroup(udg_UnitGroup_Temp)
-

Unit - Order (Triggering unit) to Attack Unit_Target
In this example, the game will add all enemy units in a group, and then for each unit, calculate the distance between them and your unit. If it's less than the previous one, it now serves as a base for the least distance, if not, it's ignored and goes to the next, etc etc. Ultimately, the unit stored in the variable is the nearest target
Just change the distance if you want a bigger radius check, and the conditions of the unit group if you need to be more precise
If you just want to attack a random target, set up a basic unit group like this and you're good to go
-
Set VariableSet UnitGroup_Temp = (Units in (Playable map area) matching (((Matching unit) is A structure) Equal to True))
Change (Playable map area) to be your region or something else, same for the conditions.