About 'Target Closest Enemy'!

In this video (objectives)…

  1. Revise instantiating
  2. Challenge you to add audio to the game now
  3. Modify Tower.cs to find closest enemy
  4. Challenge you to write Transform GetClosest(Transform b, Transform b)

After watching (learning outcomes)…

Find the closest of two transforms.

(Unique Video Reference: 27_RR_CU2)

We would love to know…

  • What you found good about this lecture?
  • What we could do better?

Remember that you can reply to this topic, or create a new topic. The easiest way to create a new topic is to follow the link in Resources. That way the topic will…

  • Be in the correct forum (for the course).
  • Be in the right sub-forum (for the section)
  • Have the correct lecture tag.

Enjoy your stay in our thriving community!

This lecture was just fine. It was a lot to take in for me. I needed to go over the code line by line three times before I really grasped how it returned the closest enemy, but that is more “me” than “you”.

Possible problem:
The enemies still don’t child to the Enemies parent object. I got the impression that they should from the beginning of the video.
I used:

GameObject enemy = Instantiate(enemySpawn, transform.position, Quaternion.identity);
enemy.transform.parent = gameObject.transform;

I did it like we did in the previous section.

[SerializeField] Transform parent;

EnemyMover enemyHierarchyInstance = Instantiate(enemyToSpawn, transform.position, Quaternion.identity);

enemyHierarchyInstance.transform.parent = parent;

I’ll have to reconsider the naming though hh

I feel that it’s redundant to check the distance to the winner every time. Doing this basically doubles the computation speed since it’s called every frame on update. It won’t make a huge difference, but when I see something that’s not as efficient as it can be, I just need to fix it. Minor inefficiencies can add up to noticeable performance issues.

Instead, I just store the min distance and the closest enemy, and calculate only the distance to the candidate enemy at every iteration. If it’s less than the min distance, that becomes the new closest enemy and min distance.

I have a problem at Tower.cs

it is related to this part:
if (sceneEnemies.Length == 0) { return; }
Transform closestEnemy = sceneEnemies[0].transform;

and this is the error that I am getting:
error CS1061: Type EnemyDamage' does not contain a definition forLength’ and no extension method Length' of typeEnemyDamage’ could be found. Are you missing an assembly reference?

Would you please help me.

Hi @matin,

My guess is that when you are defining sceneEnemies you are using FindObjectOfType<> instead of FindObjectsOfType<> (note objects is plural). The first one returns the first object it can find of that type and we can’t call .Length on an object (not that I know of anyways). The second returns all objects of that type in an array, which is why we can call .Length on it. I hope that fixes it. If it doesn’t repost with the whole function so we can see sceneEnemies in context.

Good luck!

Hi @Jeepty
Thanks for your quick response. I used the correct one (FindObjectsOfType() ) , but it is not working.

Appreciate of your help,

Okay @matin , in Pathfinder.cs the code is correct but the error is coming from line 35 in tower.cs. You’ll find that it is using FindObjectOfType. Fix that and you should be good to go! :+1:

Thanks, It is such a silly mistake :slight_smile:

I appreciate your time,

Glad I could help!

Just something I like to do: instead of using the “forever” comment, would be better to do something like this, in my option:

bool forever = true;
while (forever)

Code complete ftw :slight_smile:

Privacy & Terms