How to get positions of all other players in room from master client

Options

I am trying to make an enemy script which attacks all of the players. The only thing im missing is to make the enemy find the players. It is controlled by the master client. I have tried using GameObject.FindGameObjectsWithTag("Player"); and colliders with physics.overlapsphere and everything i can think of. The only reason it is not working is because that the player object that you see when you are in a game are in "DontDestroyOnLoad", while the enemy is not. The AI is instantiated from the Master Client using PhotonNetwork.InstantiateSceneObject.

Here is my code:


using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.XR;

using UnityEngine.AI;

using Photon.Pun;

using Photon.Realtime;


public class Sander : MonoBehaviourPun

{

    bool isHost = false;

    bool connected = false;


    float Timer = 30f;


    bool Chasing;

    int playerIndex;


    GameObject[] players;

    public NavMeshAgent sander;


    Vector3 sanderDes = Vector3.zero;


    int prevPlayerCount;


    GameObject playerChasing;

   

    void Update() {

        Collider[] colliders = Physics.OverlapSphere(transform.position,15f);

        foreach (Collider col in colliders) {

            print(col.name);

        }

        if (PhotonNetwork.IsConnected && !connected) {

            //UpdatePlayerList();


            connected = true;

            if (PhotonNetwork.IsMasterClient) {

                isHost = true;

            }

        }

        if (isHost) {

            if (prevPlayerCount != PhotonNetwork.PlayerList.Length) {

                prevPlayerCount = PhotonNetwork.PlayerList.Length;

                //UpdatePlayerList();

            }


            if (!Chasing) {

                Timer -= Time.deltaTime;

                if (Timer <= 0) {

                    sanderDes = new Vector3(Random.Range(130.123f,628.5f), -52.5f, Random.Range(267.005f,-231.59f));

                    sander.destination = sanderDes;


                    Timer = 45f;

                }


                /*float mindis = 99999999f;

                GameObject closestPlayer = null;

                foreach (GameObject pl in players) {

                    float dis = Vector3.Distance(transform.position,pl.transform.position);

                    if (dis < mindis) {

                        mindis = dis;

                        closestPlayer = pl;

                    }

                }


                if (mindis < 27f) {

                    Chasing = true;

                    playerChasing = closestPlayer;

                }*/

            }/* else {

                sanderDes = playerChasing.transform.position;

                sander.destination = sanderDes;


                if (Vector3.Distance(playerChasing.transform.position, transform.position) <= 4f) {

                    //playerChasing.transform.position = new Vector3(135.5f, -52.7f, 16.1f);

                    Chasing = false;

                    Timer = 0f;

                }

            }*/

        } else if (!isHost && connected) {

            sander.enabled = false;

        }

    }


    void UpdatePlayerList() {

        players = PlayerGameObjects.getPlayers();

    }

}