Get an array of players with GetComponent<>(); [UNET]

I have an AI that follows player gameObject that I got from a tutorial from youtube.
Until now I had no problem as I was getting the player gameObject using public.
Now I’m adding multiplayer and spawning players and I need to get an array of player gameObjects
so I’ll be able to tell my AI to chase the closer one.

#Player Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class PlayerManager : MonoBehaviour {

    #region Singleton

    public static PlayerManager instance;

    void Awake()
    {
        instance = this;
    }

    #endregion

    public GameObject player;
    // GameObject[] player = new GameObject[NetworkServer.connections.Count]; 

    void Start()
    {
       // for each player -> player *= GetComponent<GameObject>();*

}

}
AI Script:
public float lookRadius = 10f;
Transform target;
NavMeshAgent agent;

  • // Use this for initialization*
  • void Start () {*

target = PlayerManager.instance.player.transform;
agent = GetComponent();

  • }*

  • // Update is called once per frame*

  • void Update () {*
    float distance = Vector3.Distance(target.position, transform.position);
    if(distance <= lookRadius)
    {
    agent.SetDestination(target.position); // If in radius start chasing the player
    if(distance <= agent.stoppingDistance)
    {
    //attack the target
    FaceTarget();
    }
    }

  • }*

#I’d like to get to the AI Script an array of the players joined.
*Edit 1:
I tried having a list in the AI’s class of the players gameobject and in the player manager
on start add to the player gameobject list (this.transform) so every player that enters will
be added to the list. however unity just crashes to this.

You seem to have a PlayerManager. Why not keep a list of spawned players inside and make it public?