- Home /
Coroutines randomly stop working
Hello,
I'm currently working on a First Person Shooter game with Unity. I made a simple AI system for enemies which consists of a sequence which is basically:
1) Find all covers at certain range;
If the player is in a range of 15 meters, shoot at them;
2) Randomly choose one of them and run that way;
3) After a few random range of seconds (or when the cover is reached) stop, crouch and pick a target (either the player or an enemy NPC);
4) Aim at the target and shoot (with certain random imprecision) for a few random seconds;
5) Repeat.
I've wrapped this sequence in an IEnumerator, which is called in the Start () function and then calls itself at the end. There is also an obstacle-avoiding system, which uses a series of raycast to go around obstacles.
Everything seems to work fine and there are no Console errors. I can instantiate a bunch of enemies and they just work. But sometimes (actually almost every time I play) one or two enemies (out of 15) start misbehaving. They do not get killed (even though the colliders are working, since blood gets instantiated) and when they get to an obstacle they just keep stuck on it.
It seems so random, there are no errors in the code at all. The performance is fine (average 50 FPS) and it's not a memory problem (1.3GB occupied out of 8GB). Could it be C#/Mono? Or something related to the code? Any guesses?
Have you tried to use InvokeRepeating:
https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.InvokeRepeating.html
Or even just some basic Time.deltaTime timer, just to confirm if the problem is actually with the coroutine.
Your answer
Follow this Question
Related Questions
Can I make Money collecting script without attaching it to a object ? I tried but I got error 2 Answers
I'm trying to allow my character to only jump when grounded 1 Answer
Lerp in Coroutine (Crazy Behavior) 2 Answers
How can i avoid the enemy to sight me through walls? 1 Answer
problem with health pick up its going above the max health 1 Answer