- Home /
How change target with GameObject.FindWithTag
I have a turret and should do:
-when see an enemy shoot, when he is out of range or dead(destroyed), turret must target next enemy.
But my script is doing:
-target first enemy and only target rest when first dead
function Update()
{
target = GameObject.FindWithTag("Enemy").transform;
if(target && range)
{
var rotate = Quaternion.LookRotation(Objetivo.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
if (nextShotTime <= Time.time)
{
Shoot();
nextShotTime = Time.time + timeBetweenShots;
}
}
}
I need turret target next enemy when 1st out of range
Answer by craigaw · Jan 06, 2014 at 07:14 AM
The FindWithTag command will only return the first object that Unity finds with that tag. It doesn't take into account distance from the origin or any other object.
If you have to search by tags then use FindGameObjectsWithTag, this will return an array of objects, then simply add a check to find the first of those objects that is in range, or sort them by range first and pick the closest.
var objs : GameObject[] = GameObject.FindGameObjectsWithTag("Respawn");
for (var obj : GameObject in objs) {
if (range) {
target = obj.transform;
break;
}
}
Answer by 07Mr07 · Jan 06, 2014 at 12:12 PM
Im trying this, but 'objs' is always empty :/
function Update()
{
var objs : GameObject[] = GameObject.FindGameObjectsWithTag("Enemy");
//Debug.Log(objs);
for(var obj : GameObject in objs)
{
distance = (obj.position - tower.position).magnitude;
if(distance<=range)
{
shootRange=true;
}
else
{
shootRange= false;
}
if(shootRange)
{
target = objs.transform;
break;
}
}
}
FindGameObjectsWithTag
returns an array, not a single GameObject
. That's why you need to use a for
loop to iterate through it and the break
command when you find a GameObject
within range.
So when you use the Debug.Log
command to view it, it will always appear empty, something like GameObject[]
. It doesn't mean it's empty, it just means that the Debug.Log
command won't output the contents of the array. If you use Debug.Log
inside of the for
loop ins$$anonymous$$d (e.g. Debug.Log(obj)
), you'll see it returns something (assu$$anonymous$$g you have at least one object tagged as Enemy).
Also, your line defining the target
is incorrect. At the moment, it is trying to return the transform of the array, ins$$anonymous$$d of an individual GameObject
. Try using target = obj.transform;
ins$$anonymous$$d.
Answer by gonzalocastillocabrera · Jun 04, 2020 at 06:30 PM
From the YTChannel 'Brackeys'.
public Transform target;
public GameObject[] enemies;
enemies = GameObject.FindGameObjectsWithTag("Zombie");
float shortestDistance = Mathf.Infinity;
GameObject nearestEnemy = null;
foreach(GameObject obj in enemies) {
distance = Vector3.Distance (obj.transform.position, this.transform.position);
if(distance < shortestDistance){
shortestDistance = distance;
nearestEnemy = obj;
}
if(nearestEnemy != null && shortestDistance <= 9.0){
target= nearestEnemy.transform;
}
}
//This is mine
//Look Enemy
if (target != null){
Vector3 relativePos = objetivo.position - transform.position;
Quaternion rotation = Quaternion.LookRotation(relativePos, Vector3.up);
transform.rotation = rotation;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613125617im_/https://answers.unity.com/themes/thub/images/avi.jpg)