Client

Client

The client is a game instance that is connected to a server game instance, it receives all network packets the server wants to send it, so a character position is set by the server so every client sees your player in the same position.

Code that runs on the client

Logic that will only run on the client game instance. Usually in Unity game development we use attributes for defining those rules, they are little tags that go above the method declaration, which does depend a lot on the networking framework you’re using in your game.

We are using FishNet Networking, the attribute is [Client], but code that is not sent to either clients or the server is always local to that game instance, so you only use that when you want to prevent the server game instance running that code.

Client attribute

[Client]

The client attribute prevents the server from running a method.

Examples

Initializing a game screen
[Client]
private void Setup()
{
    LastScreen = ScreenType.None;

    if (_canvasGroup != null)
    {
        bool foundCanvas = TryGetComponent(out CanvasGroup canvasGroup);
        _canvasGroup = foundCanvas ? canvasGroup : GameObjectCache.AddComponent<CanvasGroup>();
    }

    SetScreenState(ScreenType.Lobby, true);

    ChangeGameScreenEvent.AddListener(HandleChangeGameScreen);
    ChangeCameraEvent.AddListener(HandleChangeCamera);
}

The [Client] attribute is preventing a server from preparing an UI, as it should not exist in a headless server. The server doesn't play the game, it manages the game.

Last updated