Problem is not reproducible or outdated
FindGameObject assign in a variable? help!
Hello! I just need help on this ... how do I automatically assign an object with the same tag when another one is destroyed? (EnemyTarget) Thanks!
using System.Collections;
public class LookTarget : MonoBehaviour {
public Transform[] target;
void Update()
{
target = GameObject.FindGameObjectsWithTag("TargetEnemy");
transform.LookAt(target);
}
}
Answer by tormentoarmagedoom · Jan 30, 2019 at 02:29 PM
Good day.
First, must know that there are these 2 functions:
GameObject.FindGameObjectsWithTag() -> Objects , with S - Return an array of all ACTIVE objects in the scene with that tag
GameObject.FindWithTag() -> Return the first ACTIVE object found in the scene with that tag
So, if you want any other object, choose the 2nd option, but if you want to decide wichone to select, use something like:
foreach (GameObject ObjectFound in GameObject.FindGameObjectsWithTag("TheTag"))
{
if (ObjectFound.something < SomeValue)
{
Something
}
}
So before destroy the object, store its tag in a string variable, to use it later in the Finding method!
Bye!!
Answer by Magso · Jan 30, 2019 at 02:32 PM
You've set an array for target and not specified which target to look at. If it's just a single target, check if target is null like this
public Transform target;
void Update()
{
if(!target)
{
target = GameObject.FindWithTag("TargetEnemy").transform;
}
transform.LookAt(target);
}
Hello! .. is something similar, only that it is with all the objects that have a "Target" tag and depending on the distance from them. Thanks!