I need to store an index on the server and have the server manage it so the clients can request that index and not get duplicates. What’s the best way to do this using UNET?
Another words:
server has index of 0, first client gets requests the index and server increments it to 1, next client gets 1 and the server increments it to 2, etc.
I see lots of info about synchronizing on all machines but not so much about specific host/server variables.
Do you mean that you basically want to assign a number to each player? All your players already have their own unique identifier, called ‘netId’ that comes with NetworkBehaviour. And in that case, you can keep a list of all client objects on your local machine and increment and decrease the number as players leave. Just as an example, this is what I did:
public class PlayerNetworked : NetworkBehaviour
{
public static PlayerNetworked localPlayer;
public static List<PlayerNetworked> allPlayers = new List<PlayerNetworked>();
public override void OnStartClient ()
{
base.OnStartClient ();
name = "Player #" + netId.ToString();
allPlayers.Add(this);
IngameInterface.instance.SetPlayerAmount(allPlayers.Count);
//
}
public override void OnStartLocalPlayer()
{
base.OnStartLocalPlayer();
name = "Player (Local) #" + netId.ToString();
localPlayer = this;
}
void Start ()
{
}
void Update ()
{
}
void OnDestroy()
{
if(allPlayers != null)
{
allPlayers.Remove(this);
}
if(IngameInterface.instance != null)
{
IngameInterface.instance.SetPlayerAmount(allPlayers.Count);
}
}
}