Why isn't potentialTargList.transform.position not being reconized?

Hi all and thanks for looking.

I’m getting a error when referencing transform.position from the potentialTargList array of GameObjects. I’m trying to sort the array by distance.

Error CS1061 ‘GameObject[ ]’ does not contain a definition for ‘transform’ and no accessible extension method ‘transform’ accepting a first argument of type ‘GameObject[ ]’ could be found (are you missing a using directive or an assembly reference?)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;

public class Test : MonoBehaviour
{
private GameObject[ ] potentialTargList;

void Update()
{
potentialTargList = EnemySpawner.enemyList.ToArray();

if (potentialTargList != null)
{
FindClosestEnemy();
}

}
void FindClosestEnemy()
{

potentialTargList = potentialTargList.OrderBy(point => Vector3.Distance(this.transform.position, potentialTargList.transform.position)).ToArray();

foreach (GameObject point in potentialTargList)
Debug.Log(point.name);
}
}

Hey! Please use code tags! :slight_smile:

as you can read, the error says exactly what the problem is,
you try to access the “transform” of your “GameObject”,

aside your script will also give you a syntax error for

foreach (GameObject point in potentialTargList)

since you dont design a body for that foreach loop

I don’t want to use tags. I want more control over what happens. I’m guessing OrderBy doesn’t know how to use GameObejcts properties.

The foreach statement breaks the point variable.

I mean Use Code Tags so we can see your code better ^^,

in your linq loop your not accessing single objects, you try to access the List.transform.position which wont work,
OrderBy is well known about GameObjects since they are classes as others,

your “foreach” statement will still drop a syntax error since you dont have a body where you want to describe the desired behaviour for ea. point in the list

Got ya… (maybe)

You’re saying I need to make a more elaborate algorithm that looks at each objects distance and and then maybe build a new array in the proper order? So I can make a struct array with object and distance then sort the array. Then build a new array according to distance?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;

public class Test : MonoBehaviour
{
private GameObject[] potentialTargList;

void Update()
{
potentialTargList = EnemySpawner.enemyList.ToArray();

if (potentialTargList != null)
{
FindClosestEnemy();
}

}
void FindClosestEnemy()
{

potentialTargList = potentialTargList.OrderBy(point => Vector3.Distance(this.transform.position, potentialTargList.transform.position)).ToArray();

foreach (GameObject point in potentialTargList)
Debug.Log(point.name);
}
}

No what i am saying is written above,

your:

foreach (GameObject point in potentialTargList)
Debug.Log(point.name);

will work but i recommend using:

foreach (GameObject point in potentialTargList) {
   Debug.Log(point.name);
}

:),

also your LinQ query is written wrong:

potentialTargList = potentialTargList.OrderBy(point => Vector3.Distance(this.transform.position, potentialTargList.transform.position)).ToArray();

you try to get the Distance from “THIS.transform.position” which is fine but you compare it with your entire “LIST” which will never work since you can compare only 2 objects with ea. other,

 IEnumerable<GameObject> query = potentialTargList.OrderBy(point => Vector3.Distance(this.transform.position, point.transform.position));

Thank you for the help.

potentialTargList.transform.position

needed to be changed to

point.transform.position

I still don’t understand why, but thanks again.

becouse lets take that Line:

IEnumerable<GameObject> query = potentialTargList.OrderBy(point => Vector3.Distance(this.transform.position, point.transform.position));

you basically say “Order me the List → potentialTarget” by EACH “point” → Distance,
and then its like a foreach loop,
you need to calculate the distance foreach point.transform.position,

it loops through the list 1 by 1 , not all at the same time,
i hope my explaining skills are not to embaressing X)