Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

114
Visualizações
See other tag but not his own

Is it possible to call all the objects in an array? Or is using the number the only way?

public void Update()
{
    Targetting ge = (Targetting)target.GetComponent("Targetting");
    enemies = ge.targets;

    float attackDistanceP = Vector3.Distance(target.transform.position, transform.position);

    foreach (Transform enemy in enemies)
    {
        float enemyDistance = Vector3.Distance(enemy.transform.position, transform.position);
        print(enemyDistance);
    }

    Debug.DrawLine(transform.position, waypointPosition, Color.black);

    if (attackDistanceP < 8)
        disableNow = true;

    if (attackDistanceP > 8)
        disableNow = false;

}

I want to call all of the objects in the enemies[] array. So that all of the enemies can see each other. But I don't want to them to see themselfs. Im using the GameObject.FindGameObjectsWithTag to fill the array in an other script.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

foreach(Transform enemy in enemies){
    if (enemy == this.transform) continue; // skip me

    float enemyDistance = Vector3.Distance(enemy.transform.position, transform.position);
    print(enemyDistance);
}
over 4 years ago · Santiago Trujillo Relatório

0

Some premature optimization for you:

using System.Collections.Generic;

public Transform trans;
Targetting ge;

void Start() {
    trans = transform;
    ge = target.GetComponent<Targetting>();
}

void Update () {
    enemies = ge.targets;

    float attackDistanceP = (target.trans.position - trans.position).sqrMagnitude; // save a square root

    foreach(Transform enemy in enemies){
        if (enemy == trans) continue; // skip me

        float enemyDistance = (enemy.trans.position - trans.position).sqrMagnitude; // save a square root
        print(enemyDistance);
    }

    Debug.DrawLine (trans.position, waypointPosition, Color.black);

    if(attackDistanceP < 64)
        disableNow = true;

    if(attackDistanceP > 64)
        disableNow = false;
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda