Multiple Enemies Cause Different Behavior

Hi there,

I’m having a problem when I add multiple enemies, the behavior changes.

When I have single enemy, everything works fine. When I add multiple enemies, they end up not detecting collision on the walls or when the trigger exit takes place and they just keep walking to the right or in some cases, they get stuck in a back and forth animation lock. Screenshot below:

My enemy on the left, walks through the platform tiles and the other is locked in a back and forth animation.

Please note, I had to utilize the multiple collider method outlined in some of the other discussions surrounding the enemy behavior as the single “periscope” collider technique did not work for me.

Right now, I have three scripts for enemy movement - one for the movement itself. One for the wall collider detection, the other for the ground collider detection.

Script screenshots below:

Can someone please explain what the reasoning is for when I add another enemy into the level that the behavior changes.

I cannot figure it out.

Any help you could provide would be greatly appreciated!

Thank you! :slight_smile:


You can’t use FindObjectOfType here. Unity searches the whole hierarchy for that object and returns the first one it finds. Each enemy has its own EnemyMovement, but you keep finding the same enemy’s one, so it doesn’t work. You probably need to use GetComponent instead. That will find the EnemyMovement on the current game object, not the whole game

I see your colliders are on child game objects. In that case you could create a serialised field and assign the EnemyMovement in the inspector

Hi Bixarrio,

Thanks for responding!

Hmm - so I tried both and neither worked.

When I use GetComponent instead of FindObjectsOfType I get an error message stating “object reference not set instance of object” - when I read up on this online, it seems that if I call GetComponent for a GameObject, Unity doesn’t like that. But I could have just misread the article.

When I utilize SerializeField as you suggested, the colliders don’t seem to work again.

I found something weird while prepping a screenshot for this post.

Before I start the game, this is what I see:

Notice that the EnemyMovement script pertains to the Enemy that it is a parent of. When I hit play:

It changes to the OTHER enemy in the hierarchy. Again, creating the same problem you noted above.

So maybe FindObjectOfType is screwing this up as you mentioned and I need to use GetComponent, but I’m just screwing it up for some reason?

Any thoughts or suggestions?

Is there something I’m missing regarding how to use GetComponent for this application?

Thank you! :slight_smile:


Not sure what article that is, but Unity made that, so I’m not sure how it won’t ‘like’ it. This is what we use.

Did you remove the FindObjectOfType from the scripts? It should not be there. Remove all traces of FindObjectOfType from the WallCollider and GroundCollider scripts. You are assigning the correct EnemyMovement and the replacing it with the wrong one again as soon as the code runs.

GetComponent gave you an error because the EnemyMovement script is not on this game object (that’s why I made the edit). It’s on the parent game object. It’s easier to create a serialised field and assign it in the inspector, but you could also have used GetComponentInParent to get the EnemyMovement.

Ahhhh I see what you mean!

Yes, this worked! Thank you so much for the help!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms