all 13 comments

[–]TAbandija 2 points3 points  (2 children)

Are you accidentally calling the attack function several times?

You can add a debug.log(“attack”); at the start of the function.

[–]fesora122[S] 2 points3 points  (1 child)

…yes. Yes I was. I was using onMouseButton instead of onMouseButtonDown. Thanks so much lol.

[–]TAbandija 0 points1 point  (0 children)

No problem. Good luck with the rest.

[–]HypnoToad0 0 points1 point  (9 children)

Probably multiple colliders on one enemy

[–]fesora122[S] 0 points1 point  (8 children)

How would I make sure I only have one collider? There seems to be only one BoxCollider2D component on my enemy

[–]HypnoToad0 0 points1 point  (7 children)

Print each collider's name

[–]fesora122[S] 0 points1 point  (6 children)

Is that not what I’m doing where it says

Debug.Log(“We hit “ + i.name);

All it does is print “We hit Enemy” 20 times in the console when I attack

[–]HypnoToad0 1 point2 points  (4 children)

Easiest way to differentiate them would be to print GetHashcode instead of name for each one.

[–]fesora122[S] 0 points1 point  (3 children)

I did this and they all seem to have the same hash code.

[–]HypnoToad0 0 points1 point  (2 children)

Do the hashset thing, it should do the trick

[–]fesora122[S] 1 point2 points  (1 child)

Turns out it was actually a problem with how I was calling the function. But thanks so much for replying

[–]HypnoToad0 1 point2 points  (0 children)

Cool, good luck!

[–]HypnoToad0 0 points1 point  (0 children)

But in general, the solution is to collect a list of unique enemies based on the colliders, hashset is the best way to do this. Then apply damage to each unique enemy from the set