How to get player game objects of network on server?
Hello,
I am trying to understan networking api in Unity. My aim is to get players' gameobjects on server to locate positions of player objects of all clients. I wrote following script for my purpose.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class PlayerPositionBroadcaster : NetworkBehaviour
{
static List<GameObject> players;
// Use this for initialization
void Awake () {
if (!isClient)
{
players = new List<GameObject>();
Debug.Log("Creating new players list");
}
}
public override void OnStartClient() {
Debug.Log("on start client");
base.OnStartClient();
if (!isServer)
CmdRegisterPlayer(gameObject);
}
void OnConnectedToServer()
{
CmdRegisterPlayer(gameObject);
Debug.Log("Conntected to server");
}
[Command]
void CmdRegisterPlayer(GameObject player)
{
Debug.Log("Registering player");
if (!isClient) players.Add(player);
}
}
My purpose was creating a list of players in server script. And make clients to register to that list when they connected to server. I made that registration with a command. Note that this script is attached to player object.
But this script gives "Trying to send command for object without authority." error.
I am wondering what is the most efficient way to get players of clients on server?
Answer by ShadowGamer3078 · Nov 27, 2020 at 04:00 PM
@umurcg I am also trying to achieve the same thing Maybe you can use GameObject.Find("Playerprefab_name (" + int.Tostring() + ")")
Then int++; It should work in theory