Spawned Unit Targeting

Hello Guys,
We are making a tower defence game. Every 30 secs, a unit is spawned from the castle. We try to make the spawned unit, target of our hero. However, the spawned units are prefabs and when we assign the prefab as the target we can’t get the result. It may be because of the spawned units being named as Object(Clone), however; we did’t make it. Any help is welcomed.

If you are only spawning one enemy at a time, you can tag it as “enemy” or something. Then you can set the target with- var target = FindGameObjectWithTag(“enemy”);

Generally, in a tower defense game, turrets aim at the closest enemy. So, you can have your enemies tagged as “Enemy” and use a function that search for the closest one among them.

function FindClosestEnemy() : GameObject { 
    var targets : GameObject[]; 
    targets = GameObject.FindGameObjectsWithTag("Enemy"); 
    var closest : GameObject; 
    var distance = Mathf.Infinity; 
    var position = transform.position; 
    for (var actual_target : GameObject in targets) { 
       var distance_difference = (actual_target.transform.position - position); 
       var current_distance = distance_difference.sqrMagnitude; 
       if (current_distance < distance) { 
         closest = actual_target; 
         distance = curDistance; 
    return closest; 

Just call this procedure in the Update() function; since it returns a GameObject, you can use this as the target of your turrets.

Thank you very much guys! We will try these as soon as possible.