Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

113
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!