performance: single "ai manager" script instead of scripts on all AI gameobjects?

hi! at the moment all of my enemies have an AI script attached to them, and this is pretty expensive- are there any examples of how a single script on a single game object can control many enemies?

i dont think you can acctually do that anywhere…
you need to minimize all the calculations of the AI’s.
i have done it and using 1 AI script for 50 enemies in the scene, and my FPS wont go below 800.
the script has around 1000 lines.
my suggestion is you should use coroutines.
like, check every 5 seconds the distance between you and the player instead of every frame.
use RIGIDBODY instead of a Character Controller as a controller for enemies.
modify the velocity like this:
velocity = Vector3(0, 0, 5); ← doing this just once will cause the enemy go straight without stopping. so you wont need to modify it every frame, just once.
things like this will sure optimize your game.
Good luck

In the end your AI has to run the code for each entity so having it a manager makes little difference I should think.

The optimization’s suggested by Yuri are sound ideas.

Another suggestion is to give the AI and activation range the first thing you do is check to see if the player is within range if not return immediately.

From what i understand you have a single script that is attached to different enemies (not a single instance of a script controlling them through a cycle from a central location?)
right, that’s where I’m at now- I have a single script that I attach to different enemies. However, I’m gonna totally rewrite it so it doesn’t use physics at all- that should help.

Sure you can do this if you like. It will save you some cycles not having to call a bunch of different Update functions, but unless you have a LOT of enemies, the difference will likely be pretty small. Just keep lists of all the enemies and on your Manager script’s Update, iterate through this list performing whatever on each of them. It’s the same way you’d traditionally do it in systems that don’t do entity/component based programming.

you dont have to use update at all. you can use coroutines instead.

You might like our product PoolManager for this. Aside from the benefits of instance pooling, it provides an up-to-date list of each pool. So if you have an enemies pool, you have access to all the active enemies in the scene. The link is in my sig if you want to see the features and a video. You can get the items or the count every frame and it will be up-to-date and very fast.

There is no reason why you can’t run a central script and use a single co-routine to run through each object and do something to it. You could even process a set number of instances per frame (yield return null every X number of iterations through the list). This should be extremely fast if you have the objects cached in a list. looping over references is nearly free performance-wise.

If you use physics properly, it should be very fast as well. I would test it both ways, but the other optimizations mentioned here would likely have a larger impact. If you don’t need physics anyway, though, you probably don’t need to use it.