FindGameObjectsWithTag is not workin...

I’m trying to make a game but my “Find GameObjectsWithTag” is not working.
Here’s my script:

var enemy : Transform;

var distance = 0.5;

var life = 100;

function Awake(){

enemy = GameObject.FindGameObjectsWithTag(“Enemies”).transform;

}

function Update () {

if(Vector3.Distance(transform.position, enemy.position) < distance){

life -= 25;

}

if(life==0){

Destroy(gameObject);

}

}

When I use this, I get this error: “FindGameObjectsWithTag can only be called from the main thread”.

It works if I use “FindWithTag” but it only looks for the first tagged enemy.

What am I doing wrong ? Please, help me…

FindGameObjectWithTag return the first GameObject that is tagged with the tag. FindGameObjectsWithTag does not return one GameObject since it returns all GameObjects with that tag. The function returns an array of GameObjects. You can’t get one transform from multiple objects.

I’m not sure what you actually want, but maybe something like this:

var enemies : GameObject[];  // now an array of gameobjects
var distance = 0.5;
var life = 100;

function Awake()
{
    enemies = GameObject.FindGameObjectsWithTag("Enemies");
}

function Update ()
{
    for (var enemy : GameObject in enemies)
    {
        if(enemy != null && Vector3.Distance(transform.position, enemy.transform.position) < distance)
        {
            life -= 25;
            if(life <= 0) // always check a range if possible
            {
                Destroy(gameObject);
            }
        }
    }
}