UNET Sending/Recieving variables

Hello,

For my internship I’m creating an interactive app that comes with my game/simulation.
Since i am absolutely new to Networking in Unity i have no clue what to do.
I’ve watched a bunch of tutorials and I’ve read the Unity Documentation about networking.

the game contains a flame, which you can control with the app, move it, rotate it, and execute some functions.
I have an working connection through UNET but i just can’t manage to change variables from client to another.
Even a simple function with the Unity UI would help me, like a button which makes my flame go up or something.

I would love to hear your people’s opinions/tips to keep me going.

Thanks in advance,

Timo

If you’re syncing a position then just attaching a NetworkTransform component to the flame should do the trick.

If you’re syncing other types of data you can either decorate it with the SyncVar attribute or use Commands and RPCs to manually sync.

public class Whatever : NetworkBehaviour
{
    [SyncVar]
    public float SomeVariable;
}
public class SomethingElse : NetworkBehaviour
{
    float someVariable;

    // called on server which sends new value to all clients
    [Command]
    public void CmdSendValue(float newValue)
    {
        RpcSetValue(newValue);
    }

    [ClientRpc]
    void RpcSetValue(float newValue)
    {
        someVariable = newValue;
    }
}

// elsewhere
somethingElse.CmdSendValue(10);
1 Like

Keep in mind that if you use RPC, the value will not be changed on clients that connect after the RPC is called.

I’m not sure that this is true - have you tested it? Server’s should be sending objects in their current state but I’m not sure how far that extends.

I have tested this. Anything that is not marked to be synchronized will be left in its default state.
From testing and research I’ve done, when a client connects, it synchronizes with the server for objects to spawn, and spawns the corresponding prefab, not the actual server-side object. Then, when the object is being initialized, and SyncList or SyncVars get updated (without calling hooks)
ClientRPC’s are one-time messages that get sent out to all connected clients. Since clients are not able to make synchronized objects marked as dirty (only the server can), any changes to values or objects are not recorded on the server, and so do not get passed to new clients. Additionally, the RPC, unless called again, does not get sent to new clients.

2 Likes