My goal is when enemy OnTriggerEnter add to the list, onExit remove from list. in this code should add a “enemy” but sometimes add all enemies when enters collider sometimes one or two not added to the list.
Second question - any tips how to do add object to scene(button) for each entered enemy? eg one enemy enters then button appear on screen, when second enemy enters second button appears and so on.
So you list the actual enemy that entered your trigger and not another one at random.
Then you should probably consider checking if the enemy isn’t already in the list before adding it (newList.Contains(…)) thus avoiding to end up with the same enemy listed several times !
Np, did you add a check before adding stuff to your list, to ensure there’s no duplicate entries ?
if (!newList.Contains(targetColBubble.transform))
{
newList.Add(targetColBubble.transform);
}
Other considerations would be about your foreach block, firstly you shouldn’t use foreach with lists but whatever let’s not get into this just yet. I don’t know how you’ve set this up but you should make sure you don’t call it several times, or if you do, remove enemies from the list otherwise each time you’ll be creating new targetSlot for each enemy.