Enemies stop appearing in world despite transform.position moving along path

After making changes in this Script Execution Order video and returning the Object Pool to 5, the enemies do not appear on screen. I put in debug.logs to detect the transform.position at various points, and the enemies populate the pool at the start of the path, and they travel along the path to the end, although some strange positions also appear. When towers are placed, the path changes and the towers look around for enemies but do not fire in any direction.

Lastly, (I think this is unrelated but) the path goes through trees, despite the tiles being blocked.

It could be that my grid and position was initially not set up correctly, so the switch between position and coordinates does not work, but it doesn’t seem to be that. Anyway, I’ve checked the code several times and I can’t find any differences. I will keep looking into the problem, but any ideas would be helpful.

Hi Alexander,

First of all, check the Hierarchy. Maybe the enemies do appear in the scene but at an unexpected position. It is also possible that they get spawned on top of one another, which would look as if there was only one enemy in the scene.

Since the other problems might also be related to the code, please compare your code to Gary’s. A link to his code can be found in the Resources > Lecture Project Changes of this lecture.

Last but not least, make sure that all coordinates of your tiles are not negative. For example, (0, 0) and (99, 123) would be valid position values. (-1, -1) and (0, -2.456) would be invalid. The reason for this is that we generate coordinate keys in a for-loop (see GenerateGrid()), and the i variable starts at 0 and gets incremented. We do not generate any keys with negative values, thus no waypoint with negative coordinates will be found in our grid dictionary.

Please let me know if this helped. :slight_smile:


See also:


Found them!

Checking the Hierarchy revealed that the enemies were being set to active in order and them going inactive when they hit the finish line and decremented the gold. The console shows them following the “right position” but when I moved the camera down to check below, that’s where they were spawning and traversing the path, well below the grass. (Not the rams in the top right, those are UI elements)

Anyway, I had some trouble setting up the map and using tiles, so I think something is funky with the position values of either the tiles or the Object Pool. I tried selecting and dragging the whole map to match the enemies, but it is not easy to line them up. Would reseting the transform of both the whole map and the Object Pool fix this? Afraid to try it just yet. Will have another look tomorrow.

Also, my coordinates are all non-negative.

Thanks for you help!

Fantastic. Your screenshot looks good. Everything seems to be there, so the solution should not be that difficult. :slight_smile:

  • Reset the Transform of the ‘Object Pool’ game object. This will make it easier to interpret the local position of the children, which are the enemies.
  • Also Reset the Transform of the root game object of the tiles.

If the parent position is not (0, 0, 0), you have to add the vector to the local position of the children to get their global position. Not in Unity but, for example, with pen and paper. Then you could calculate the offset between enemies and tiles. The Inspector always shows the local position.

Theoretically, the enemies’s global y-position is always 0 or some other constant value. If that’s the case, you just have to move the terrain down along the y-axis. Alternatively, you could edit the Vector3 of the moving enemies and give them a constant y-value, which makes them move on top of the tiles.

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

Privacy & Terms