Units becoming 'confused' when they spawn

I have this code:

Conditions:

For each object playermeleeunit: Variable moving of playermeleeunit = 0 Variable incombat of playermeleeunit = 0 The distance between playermeleeunit and enemymeleeunit < 300 pixels Variable incombat of enemymeleeunit = 0

Actions:

Unlink all objects from playermeleeunit
Stop object playermeleeunit 
Stop object enemymeleeunit 
Link playermeleeunit and enemymeleeunit 
Do = 1 to variable incombat of playermeleeunit 
Do = 1 to variable incombat of enemymeleeunit
Do = playermeleeunit.Variable(ID) to variable target of enemymeleeunit
Do = enemymeleeunit.Variable(ID) to variable target of playermeleeunit
Move playermeleeunit to enemymeleeunit with a force of playermeleeunit.Variable(movespeed)*

*Other stuff happens when the playermeleeunit reaches the enemymeleeunit.

The problem is, if a playermeleeunit spawns and there are multiple enemies in range, then ALL enemies change their combat status and targets as though they are battling the playerunit. I don’t know why this is happening? :S

Maybe you should use “consider random [enemy] object” condition if you want one enemy to target player? Just a guess. Also I’d like to see how your “other stuff” code looks like, preferably as screenshot, because that may also affect things.

For each object playermeleeunit: Variable moving of playermeleeunit = 0 Variable incombat of playermeleeunit = 0 The distance between playermeleeunit and enemymeleeunit < 300 pixels Variable incombat of enemymeleeunit = 0 Take a random enemy unit

Did work. :wink: Thx. :slight_smile:

You’re welcome :slight_smile:.