UNET (Multiplayer) calling [command] function twice
I am playing around with UNET but stuck into this weird problem/scenario while testing on same machine (unityEditor and buildRun to create "2 players/instances"
The problem is weird interaction of [Command] attribute and void OnStartLocalPlayer() when hosting the game in Editor's instance and in standalonePlayer instance
Basically, when I am host in editorInstance (host), the function associated with [Command] is triggered twice by the standaloneInstance (client)
When I am hosting in standaloneInstance (host), the line which calls the function associated with [Command] throws warning "Trying to send command for object without authority." But the function works as expected
I am spawning a PlayerObject from my NetworkLobbyManager and then that PlayerObject spawns its own pieces (sub-objects) using NetworkServer.Spawn(playerUnit);
Any help would be appreciated :( I like learning by trial and error but since 3 days I have not progressed or understood whats going on.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class playerManager : NetworkBehaviour
{
public int playerID;
public GameController GC;
public int playerUnitID;
public bool localPlayer;
public bool serverPlayer;
public bool clientPlayer;
// Use this for initialization
void Start () {
//Assign player unique ID as per order of joining
if (!GC)
{
GC = GameObject.FindObjectOfType<GameController>();
//playerID = this.GetComponent<NetworkIdentity>().connectionToClient.connectionId; connectionToClient is for serverOnly, connectionToServer is clientOnly
playerID = (int)netId.Value;
Debug.Log("(PM) Created GC in Start: " + playerID);
//CmdCreatePlayerUnit(playerID); //this gets called in OnStartLocalPlayer anyway(Trying to send command for object without authority)
}
localPlayer = isLocalPlayer;
serverPlayer = isServer;
clientPlayer = isClient;
Debug.Log("Player: "+playerID+" isLocalPlayer: "+localPlayer+" and isServerPlayer: "+serverPlayer);
}
// Update is called once per frame
void Update () {
}
public override void OnStartLocalPlayer()
{
if (!GC)
{
GC = GameObject.FindObjectOfType<GameController>();
//playerID = this.GetComponent<NetworkIdentity>().connectionToClient.connectionId; connectionToClient is for serverOnly, connectionToServer is clientOnly
playerID = (int)netId.Value;
Debug.Log("(PM) Created GC in OnStartLocalPlayer: " + playerID);
CmdCreatePlayerUnit(playerID);
}
//Debug.Log("(PM) OnStartLocalPlayer: " + gameObject.name);
Debug.Log("Player: " + playerID + " isLocalPlayer: " + localPlayer + " and isServerPlayer: " + serverPlayer);
}
[Command]
void CmdCreatePlayerUnit(int playerID)
{
Debug.Log("Creating player unit with ID: " + playerID + " and isLocalPlayer: "+isLocalPlayer);
GC.registerPlayerNetID(playerID);
Debug.Log("(PM) Registered Players: " + GC.playerNetIDs.Count);
for (int i = 0; i < GC.playerNetIDs.Count; i++)
{
//Debug.Log(GC.playerNetIDs[i]);
if(GC.playerNetIDs[i]==playerID)
{
playerUnitID = i;
Debug.Log(playerID+" player's unitID is "+i);
}
}
GameObject prefab = GC.players[playerUnitID];
GameObject playerUnit = (GameObject)Instantiate(prefab, prefab.transform.position, prefab.transform.rotation);
NetworkServer.Spawn(playerUnit);
//Update the same in GC
GC.registerPlayer1(playerUnit,(int)playerUnitID);
}
}