Search Unity

How to send variables through Unity Network?

Discussion in 'Multiplayer' started by UDN_60115ee5-2147-4fcc-8ff0-dd54695f410f, Mar 15, 2017.

  1. UDN_60115ee5-2147-4fcc-8ff0-dd54695f410f

    UDN_60115ee5-2147-4fcc-8ff0-dd54695f410f

    Joined:
    Jan 29, 2017
    Posts:
    8
    I'm making a 1vs1 multiplayer game turn-based in 2D and C#.

    This is the first time that I use the Unity Network, and there is one very simple thing that I can not solve: I have an int, and I want one of the two players to send a new value of the int to the other player, and that the other player receives and updates the int in his client.

    How would be the code to do this?

    Sorry if I have a bad english.
     
  2. UDN_60115ee5-2147-4fcc-8ff0-dd54695f410f

    UDN_60115ee5-2147-4fcc-8ff0-dd54695f410f

    Joined:
    Jan 29, 2017
    Posts:
    8
    I don't mean to send the int through two or more scripts, I say in the network.
    So I mean to have a script(or more, if necessary) for sending and receiving an integer through the Unity Network.

    An example of what I want to do: Player1 sends new someInt value, and new value sent is 2.
    Player2 has someInt value as 0, but receives a new vañue from the Network and updates the integer from 0 to 2.
     
  3. donivanbardin

    donivanbardin

    Joined:
    Mar 15, 2017
    Posts:
    9
    Last edited: Mar 15, 2017
  4. srylain

    srylain

    Joined:
    Sep 5, 2013
    Posts:
    159
    The first thing you need to realize, is that players cannot send data directly to other players. It has to through the server. You can use ClientRPC's and Command's to do this, you would send the variable from the client to the server through a Command, and then when the server receives the Command you can send it to the other client using a ClientRPC (or a TargetRPC, ClientRPC's send to every client while TargetRPC's are only sent to a single specified client).
     
    kyleshell likes this.
  5. UDN_60115ee5-2147-4fcc-8ff0-dd54695f410f

    UDN_60115ee5-2147-4fcc-8ff0-dd54695f410f

    Joined:
    Jan 29, 2017
    Posts:
    8
    After a little bit of research, I found how to do the script, and it worked 90%, but, only the host can send commands, and the client player can't.
    I don't have any player prefab or something, because my type of game doesn't need it, and I tried to use an empty GameObject with the script and NetworkIdentity and call from buttons the script of the empty gameobject, and in the console of the client player is shown this message: "Trying to send command for object without authority."

    Any ideas?
     
    ritgunnybook likes this.
  6. srylain

    srylain

    Joined:
    Sep 5, 2013
    Posts:
    159
    Clients can only send Commands through objects that they own. To own an object, that object needs a NetworkIdentity component attached along with the "Local Player Authority" checked. It also needs to be spawned in by the server either automatically or with the NetworkServer.SpawnWithClientAuthority() method (I think that's what it's called).

    You can also forgo the whole Command/RPC structure and use Messages: https://docs.unity3d.com/Manual/UNetMessages.html

    You can send Messages through any object (doesn't need any sort of authority), but they are a bit harder to use and they require more setup. If you don't need a Player object for each client, you might be better off going with Messages.