PeterK
PeterK

Reputation: 4301

How to have a property add and sync when player spawn

Have a bit of problem understanding how to have the property theNumber add and sync.

1) I have two players

2) When the players spawn I want theNumber to add one so each player report a different sequential number

I just do not get it to work and would appreciate some help.

The following code is placed on the players that are spawned.

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

public class Player : NetworkBehaviour {

[SyncVar] public int theNumber;
private int _nr;

public override void OnStartLocalPlayer () {
    print ("OnStartLocalPlayer");
    _nr = theNumber;
    CmdX (theNumber);
    DoCalc ();
}

[Command]
void CmdX (int myInt) {
    print ("theNumber: " + myInt);
}

[Client]
void DoCalc () {
    _nr++;
    CmdPrint (_nr);
}

[Command]
void CmdPrint (int nr) {
    theNumber = nr;
    print ("CLIENT CONNECTED WITH THE FOLLOWING NUMBER: " + theNumber);
}

}

Upvotes: 0

Views: 370

Answers (1)

user4148144
user4148144

Reputation:

You will want to change the value on the server when a new client connects and send it down. From the looks of it, you're modifying the value on the client only. Additionally, your [SyncVar] attribute on theNumber will synchronize the value across clients, so it may be changing it to the same value for each client.

Read more here: http://docs.unity3d.com/ScriptReference/Networking.SyncVarAttribute.html

Upvotes: 1

Related Questions