Search code examples
c#unity-game-enginemultiplayer

Unity multiplayer global variable


I have Player(NetworkBehaviour) script attached to player prefab which spawn when client join and Global(MonoBehaviour) script which is inside scene

In Global script i have this:

class Global : MonoBehaviour
{
    public static int gloablInteger;

    void Start()
    {
         globalInteger = 0;
    }
}

In Player Script i have this:

class Player : Networkbehaviour
{
    public void Start()
    {
        Global.globalInteger++;
    }
}

I set up in update function to Debug.Log(globalInteger) and it prints 0, then first client(host) join and it prints 1, then second client joins and it still prints 1 instead of 2. Why is this happening?


Solution

  • Everytime a client joins it resets globalInteger to 0 in Start() function.