Search Unity

Trying to understand the basic of networking, why it doesn't work?

Discussion in 'Multiplayer' started by flocool, Feb 9, 2020.

  1. flocool

    flocool

    Joined:
    Dec 19, 2016
    Posts:
    3
    Hello,

    I am new to Nertworking in Unity. I read many website pages, I watch some Youtube videos, but I am not able to understand why my code doesn't work ... I'm confused.

    Just to understand how it works:
    - I've created a scene with just NetworkManager + NetworkManagerHUD
    - I've created a second scene with 1 text et 1 button.
    Here is what I want: when someone click on the button, the text is updated with the elapsed time.
    Here is my code:
    Code (CSharp):
    1.  
    2. [SyncVar]    public float toto = 3;
    3.     [Command]
    4.     public void CmdChange()
    5.     {
    6.         toto = Time.time;
    7.         RpcChangeValue(toto);
    8.     }
    9.  
    10.     [ClientRpc]
    11.     void RpcChangeValue(float value)
    12.     {
    13.         GameObject.Find("TextPanel").GetComponent<Text>().text = value.ToString();
    14.     }
    When I click on the button, it calls the "CmdChange" method.
    If I click from the server, the text change on both server and client. But if I click from the client, nothing change. If the client is Unity, here is the message I have in the console:
    => RPC Function RpcChangeValue called on client

    I really don't understand, I've followed a tutorial where the guy did the exact same thing (but he doesn't use a button). Do you have any idea?

    Thanks.
    I really want to understand how it works, to implement Networking then on the board game I've developed on Unity.

    Btw, I know that NetworkBehaviour is deprecated, but we don't have any other package/class/method from Unity if I'm right.
     
    Last edited: Feb 9, 2020
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    On the client are you getting a warning about trying to send a Command from an object the client doesn't have authority over? In Unet only a client with authority over the networked GameObject can send Commands, unless it is that client's special Player GameObject which it can use to send Commands even if you are using server authority for the object.

    So usually a problem sending a Command is because you're trying to send a Command on a GameObject that client doesn't have the authority to do so.

    But at this stage, don't just now try to learn Unet. If you like Unet and want to use Unet tutorials I'd suggest taking a look at Mirror, which is a fork of Unet with a bunch of fixes and actual support but most Unet tutorials will still work other than changing some "using" code at the top of the script.
     
  3. flocool

    flocool

    Joined:
    Dec 19, 2016
    Posts:
    3
    Thanks @Joe-Censored for your reply.
    It is what I thought about. In that way, I've had the component "Network Identity" to my button and checked the button "Local player Authority". But it didn't solve my issue. I still can't click on my button.

    I've heard about Mirror, I will have a look soon then.
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    How are you spawning the networked object? Normally you'd spawn the object on the server using NetworkServer.SpawnWithClientAuthority and pass in the connection to the client you want to have authority. If the object is just a part of the scene for example, then the server wouldn't know what client to assign authority, so I wouldn't be surprised if authority is still the server. You can test this by checking hasAuthority.

    Code (csharp):
    1. void Start()
    2. {
    3.     if (hasAuthority)
    4.     {
    5.         Debug.Log("hasAuthority is true");
    6.     }
    7.     else
    8.     {
    9.         Debug.Log("hasAuthority is false");
    10.     }
    11. }
    https://docs.unity3d.com/2018.1/Doc...g.NetworkServer.SpawnWithClientAuthority.html
    https://docs.unity3d.com/2018.1/Doc...Networking.NetworkBehaviour-hasAuthority.html
     
  5. flocool

    flocool

    Joined:
    Dec 19, 2016
    Posts:
    3
    It's just a button in the scene:

    Can I spawn just a button?
    How can I make a button clickable for every player?

    I've coded a whole board game where we can move meeples / monsters along tiles. Each player can move monsters when it is his turn. Regarding meeples, of course you can only move yours, but it is already coded. I will have to change that for sure, regarding the connected player.
    Anyway, I just wanted to understand how a button can be clicked by every connected players. It sounds so weird.
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I don't think it is a good idea to make your UI itself a network spawned GameObject. What I would do is make the UI local to the client and in a Start method on a script attached to your special Player GameObject, check for isLocalPlayer, and if true have it find your UI and give your UI a reference to itself. Then when players press buttons, the UI uses that reference to call a Command on that client's own Player GameObject. This Command is guaranteed to go through correctly.

    Alternatively I would just use Unet Messages, which actually makes more sense because UI clicks are probably not really associated with specific game world objects but are probably more associated with the client itself. Unet Messages don't have to deal with the object authority system since they aren't associated with specific GameObjects.