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}");
}
}
If you have a
NetworkVariable<T>not syncing, it could be any of the following:Make sure your class is inheriting from
NetworkBehaviour, notMonoBehaviour.Make sure the GameObject your script is attached to also has a
NetworkObjectcomponent:If not, click on "Add Component" and add the
NetworkObjectscript.Local object references just cannot be synced. Try to make them
NetworkObjectsor sync them as ID numbers instead. (Unlike the other 2, this should print error logs.)