Our list is getting rewritten

We are calling the GetPath method everytime a new enemy spawns, which is not a good thing, every new enemy is rewriting the “path” list making the previous one obsolete, even if it is the same, that’s why we are getting this error “Collection was modified; enumeration operation may not execute.” basically telling us that our coroutine will not execute, the same things happens with every dictionary and list we have, that’s why we also get debug code telling us that it is ignoring all the blocks.

The fix is actually quite simple, create the path, then let the enemies access it, there’s no need to create a path every single time an enemy spawns. This also can helps us create several paths if needed with a list of lists.

So, I decided to experiment a bit before seeing what Ben had to say about this matter and see if I could get the solution for this.

The simpliest solution I found was to check if the path was already created, if it wasn’t then create a new one, if it was then return the already created path, that added 4 lines of codes because of the if statement brackets, but it could have done in a single line.

The downside of this solution is that it will not work if you want multiple spawn points, for that we need a much more elegant solution.


Privacy & Terms