Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Clients Communicating with a Server Singleton

Discussion in 'Multiplayer' started by MarxentAnthony, Jul 1, 2015.

  1. MarxentAnthony

    MarxentAnthony

    Joined:
    Feb 20, 2015
    Posts:
    18
    I am having problems sending Commands from a Client to a Server Singleton

    The Player prefab has a NetworkBehavior called NetworkSetup on it which implements OnStartLocalPlayer(). In that function I find the Singleton NetworkProductManager (derived from Network Behavior with NetworkIdentity) in the mission with a GameObject.Find().getComponent().


    Then later the client uses a local Canvas (also on the Player prefab) to call NetworkSetup.RequestLoadProduct() which in turn calls NetworkProductManager.CmdLoadProduct() ( a Command).

    On the host the function is called but on remote clients I get this error

    Below is the relivant code snippet. Can someone point out what I am missing or doing wrong?
    Code (csharp):
    1.  
    2.  
    3. public class NetworkSetup : NetworkBehaviour {
    4.  
    5. [SerializeField] NetworkProdcutManager netProdMan = null;
    6.  
    7. public override voidOnStartLocalPlayer ()
    8. {
    9. netProdMan = GameObject.Find ("ProductManager").GetComponent<NetworkProdcutManager>();
    10. if (netProdMan == null)
    11. {
    12.   Debug.Log ("did not find netProdMan");
    13. }
    14. base.OnStartLocalPlayer ();
    15. }
    16.  
    17. public void RequestLoadProduct()
    18. {
    19. if (netProdMan == null)
    20. {
    21. Debug.Log ("RequestLoadProduct:: Error no Network Product Manager");
    22. return;
    23. }
    24. if (!isLocalPlayer)
    25. {
    26. Debug.Log("RequestLoadProduct:: Only call for Our Client");
    27. return;
    28. }
    29. Debug.Log ("Local RequestLoadProduct");
    30. netProdMan.CmdLoadProduct ();
    31. }
    32.  
    33. }
    34.  
    Code (csharp):
    1.  
    2. [RequireComponent (typeof(NetworkIdentity))]
    3. publicclass NetworkProdcutManager : NetworkBehaviour
    4. {
    5. [Command]
    6. publicvoidCmdLoadProduct()
    7. {
    8. Debug.Log ("NetworkProdcutManager::LoadProduct isServer: "+ isServer + " isLocalPlayer: " + isLocalPlayer);
    9. }
    10. }
    11.  
    Code (csharp):
    1.  
    2. public class DebugCanvas : MonoBehaviour {
    3.  
    4. private NetworkSetup nSetUp = null;
    5.  
    6. voidStart()
    7. {
    8. nSetUp = GetComponent<NetworkSetup> ();
    9.  
    10. }
    11. //Client Only
    12. public void onLoadProductButtonPressed()
    13. {
    14. Debug.Log ("onLoadProductButtonPressed");
    15. nSetUp.RequestLoadProduct ();
    16. }
    17. }
    18.  
    19.  
     
  2. MarxentAnthony

    MarxentAnthony

    Joined:
    Feb 20, 2015
    Posts:
    18
    So I figured it out

    Code (csharp):
    1.  
    2. public class NetworkSetup : NetworkBehaviour {
    3. //Client
    4. public void RequestLoadProduct()
    5.  {
    6.  
    7. if (netProdMan == null)
    8.  {
    9.   Debug.Log ("RequestLoadProduct:: Error no Network Product Manager");
    10.   return;
    11.  }
    12. if (!isLocalPlayer)
    13.  {
    14.   Debug.Log("RequestLoadProduct:: Only call for Our Client");
    15.   return;
    16.  }
    17.  
    18. Debug.Log ("NetworkSetup::RequestLoadProduct isServer:" + isServer );
    19.  
    20.  
    21.  
    22.   CmdLoadProduct ("Monkey Nutz");
    23.  }
    24.  
    25.  [Command]
    26. public void CmdLoadProduct (string data)
    27.  {
    28. Debug.Log ("NetworkSetup::CmdLoadProduct isServer:" + isServer);
    29. if (netProdMan == null)
    30.  {
    31.   Debug.Log ("CmdLoadProduct::Server needs Network Product Manager");
    32.  
    33.   netProdMan = GameObject.Find ("ProductManager").GetComponent<NetworkProdcutManager>();
    34.  }
    35.  
    36. netProdMan.LoadProduct (data);
    37.  }
    38. }
    39.  
    Code (csharp):
    1.  
    2. [RequireComponent (typeof(NetworkIdentity))]
    3. public class NetworkProdcutManager : NetworkBehaviour
    4. {
    5.  
    6. //Executed on Server
    7. public void Load Product(string data)
    8.  {
    9. if (!isServer)
    10.   return;
    11. //Loadthingsandwhatnot
    12. Debug.Log ("NetworkProdcutManager::CmdLoadProduct load: "+ data );
    13.  
    14. RpcLoadProduct (data);
    15.  
    16.  }
    17. //Executed on Client
    18.  [ClientRpc]
    19. voidRpcLoadProduct(string data)
    20.  {
    21.     Debug.Log ("NetworkProdcutManager::RpcLoadProduct load: "+ data);
    22.  }
    23. }
    24.