- Home /
Question by
Lewwwer · Sep 07, 2016 at 06:46 PM ·
networkingsyncclient-serverindexcommand
Problem numbering Clients
I'm trying to make a Networking game, and I want to assign each player an unique number from 0 to the player numbers-1. I made a code, but it doesn't working:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
using UnityEngine.UI;
public class OptionSync : NetworkBehaviour {
public int myClientNum = -1;
[SyncVar]
public int nextClientNum= 0;
void Update ()
{
if (isClient && myClientNum >=0)
{
GetComponentInChildren<Text>().text ="index:"+myClientNum;
}
}
public override void OnStartClient()
{
base.OnStartClient();
myClientNum = nextClientNum;
CmdCreated();
}
[Command]
void CmdCreated()
{
nextClientNum++;
}
}
The idea is to make a synced variable with the last index and to make a variable for the actual index. In the update I'm using a text to display the number. At the start of the Client I'm overriding the OnStartClient to assign the index. After that I'm calling the command to increase the synced nextClientNum. I think it should work, but when I run 3 instances I get a Client with index:0 the others has index:1.
Comment