How to get the position of a OverlapSphere sorted Linq object, second in list?

Basically I have an enemy that’s can move around the map. There’s a bunch of buildings, (71),the nearest ones in an array called Gos,which is then sorted. then it Instantiates a grapplehook prefab called dem1hook and renamed to D1hook. I want that hook (which moves forward from its own, different script) to be aimed at the second-closest gos, so I used the LookAt function or LookRotation (which give different, both incorrect results) in different ways and none of them worked. I’m lost.

here’s the instantiating/aiming Method of the script that spawns the hook:

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

public class D1grapplespawn : MonoBehaviour
{

    public GameObject[] gos;
    public GameObject closest;
    public Vector3 closestloc;
    public GameObject[] ya;
    public GameObject D1hook;
    public GameObject dem1hook;
    public GameObject dem1;
    public GameObject LeftCheck;
    lefthookcheck lefthookcheck;
    public GameObject lol;
    public float distTween;
    public float mindist;
    float hook;
    public float lasted;
    public float hooks;
    List<GameObject> someList;
    public LayerMask mask;
    D1hookend1 D1hookend1;





    void Start()
    {


        dem1 = GameObject.Find("demon1");
        LeftCheck = GameObject.Find("lefthookcheck");
        lefthookcheck = LeftCheck.GetComponent<lefthookcheck>();



    }

    void Update()
    {




        if (lol)
        {
            distTween = Vector3.Distance(lol.transform.position, transform.position);
            if (distTween > mindist && D1hookend1.attached == true)
            {
                Destroy(dem1hook.gameObject);
            }

            if (D1hookend1.hitbound == true)
            {
                hooks = hooks - 1;
                (lol.GetComponent(typeof(D1hookend1)) as D1hookend1).KillHook();
               
            }

        }
            if (D1hookend1.attached == true)
            {
                (dem1.GetComponent(typeof(demonai)) as demonai).flingtoHook();


            }
        if (hooks == 0)
        {
            SpawnHook();




        }


        lol = D1hook;




        if (D1hookend1.attached == true)
        {
            lasted = lasted + 1;
        }
        if (lasted == 20)
        {

           
            D1hookend1.attached = false;

           
            Destroy(lol.gameObject, 0);
            hooks = hooks - 1;
            lasted = 0;
        }


    }




    void SpawnHook()
    {

        gos = Physics.OverlapSphere(transform.position, 6000, mask)
            .OrderBy(x => Vector3.Distance(x.transform.position, transform.position))
            .Select(x => x.gameObject).ToArray();


        closest = gos[1];
        closestloc = gos[1].transform.position;



       

        Quaternion lookat = Quaternion.LookRotation(gos[1].transform.position - transform.position);
        transform.rotation = lookat;
        D1hook = Instantiate(dem1hook, gameObject.transform.position, gameObject.transform.rotation) as GameObject;


        lol = D1hook;
        D1hookend1 = D1hook.GetComponent<D1hookend1>();
        hooks = hooks + 1;
    }
}

}

The problem is that I can’t figure out why closestloc isn’t returning the right Vector. I tried putting gos[1] in, instead, but that didn’t work either. I tested the scene by adding " D1hook.transform = “closestloc” and it gave a completely different position from where “closest” is. What am I doing wrong, and how can I get my grapplehook to shoot towards the second closest “gos”?

Also, I’m not sure the code I’ve written really gives me the second-closest object. It definitely gives a close one though. It’s weird.

Are you even sure that gos[1] is correct target? Debug the name of the object to see if it’s right.

I did a “transform.position = gos[1].transform,position” before and it showed up at a spot that wasn’t even a Gos. it definitely wasn’t the same house as gos[1], which I can see in the inspector cuz the GameObject “closest” is public

p.s., Sorry I took so long to reply when you’re the one helping me, I lost track of time programming in unity. It’s so cool!

Technically you do closest = gos[1]; so it definitely is the same one (but then both are wrong). You need to plan the selecting closest better, maybe a for loop, going through all objects that can be targeted, and then select one which sqrMagnitude is smaller than square of highest allowed range.

Maybe in the gos[ ] is alot more objects that you didn’t notice, floor, player itself or something. I don’t know those details about the game.

Now that I look at it, it IS sorted wrongly. They’re all the right objects, though, according to the Inspector. But it’s still not going toward Closest (which is actually supposed to be second-closest btw. it’s a bit of a misnomer). It’s not going the exact opposite way either.

so there’s something wrong with both the sorting AND the aiming parts of the script.

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

public class D1grapplespawn : MonoBehaviour
{

    public GameObject[] gos;
    public GameObject secClosest;
    public Vector3 secClosestloc;
    public GameObject[] ya;
    public GameObject D1hook;
    public GameObject dem1hook;
    public GameObject dem1;
    public GameObject LeftCheck;
    lefthookcheck lefthookcheck;
    public GameObject lol;
    public float distTween;
    public float mindist;
    float hook;
    public float lasted;
    public float hooks;
    List<GameObject> someList;
    public LayerMask mask;
    D1hookend1 D1hookend1;





    void Start()
    {


        dem1 = GameObject.Find("demon1");
        LeftCheck = GameObject.Find("lefthookcheck");
        lefthookcheck = LeftCheck.GetComponent<lefthookcheck>();



    }

    void Update()
    {




        if (lol)
        {
            distTween = Vector3.Distance(lol.transform.position, transform.position);
            if (distTween > mindist && D1hookend1.attached == true)
            {
                Destroy(dem1hook.gameObject);
            }

            if (D1hookend1.hitbound == true)
            {
                hooks = hooks - 1;
                (lol.GetComponent(typeof(D1hookend1)) as D1hookend1).KillHook();
             
            }

        }
            if (D1hookend1.attached == true)
            {
                (dem1.GetComponent(typeof(demonai)) as demonai).flingtoHook();


            }
        if (hooks == 0)
        {
            SpawnHook();




        }


        lol = D1hook;




        if (D1hookend1.attached == true)
        {
            lasted = lasted + 1;
        }
        if (lasted == 20)
        {

         
            D1hookend1.attached = false;

         
            Destroy(lol.gameObject, 0);
            hooks = hooks - 1;
            lasted = 0;
        }


    }




    void SpawnHook()
    {

        gos = Physics.OverlapSphere(gameObject.transform.position, 6000, mask)
            .OrderBy(x => Vector3.Distance(transform.position, x.transform.position))
            .Select(x => x.gameObject).ToArray();


        secClosest = gos[gos.Length -2];
        secClosestloc = gos[1].transform.position;



     

        Quaternion lookat = Quaternion.LookRotation(transform.position- secClosestloc);
        transform.rotation = lookat;
        D1hook = Instantiate(dem1hook, gameObject.transform.position, gameObject.transform.rotation) as GameObject;


        lol = D1hook;
        D1hookend1 = D1hook.GetComponent<D1hookend1>();
        hooks = hooks + 1;
    }
}