xjcl
xjcl

Reputation: 15309

MLAPI NetworkVariable not syncing on clients

I'm making an online multiplayer game with Unity and MLAPI. I wrote the script below and placed it in my scene, meaning each client and server should run it when a scene loads. The server is supposed to set the variable to 1, but it's not changing on the clients!

using UnityEngine;
using MLAPI;
using MLAPI.Messaging;
using MLAPI.NetworkVariable;

public class TestNetwork : NetworkBehaviour
{
    public NetworkVariableInt Test = new NetworkVariableInt(-1);

    void Start() {
        if (!NetworkManager.Singleton.IsServer) return;
        Test.Value = 1;
    }

    void Update() {
        Debug.Log($"{Test.Value}");
    }
}

Upvotes: 1

Views: 3213

Answers (2)

Rom
Rom

Reputation: 426

The MLAPI is changing fast and this might not be relevant anymore, but also make sure you set the NetworkVariable permissions correctly:

NetworkVariable<int> Test = new NetworkVariable<int>(-1);

private void Awake() {
    Test.Settings.ReadPermissions = NetworkVariablePermission.Everyone;
    Test.Settings.WritePermissions = NetworkVariablePermission.Everyone;
}

Upvotes: 0

xjcl
xjcl

Reputation: 15309

If you have a NetworkVariable<T> not syncing, it could be any of the following:

  • Make sure your class is inheriting from NetworkBehaviour, not MonoBehaviour.

  • Make sure the GameObject your script is attached to also has a NetworkObject component:

    enter image description here

    If not, click on "Add Component" and add the NetworkObject script.

  • Local object references just cannot be synced. Try to make them NetworkObjects or sync them as ID numbers instead. (Unlike the other 2, this should print error logs.)

Upvotes: 1

Related Questions