Hello,
I’m learning how to use UNet and at the moment I’m just trying to build some simple samples that will help in the long run on the development of my application. Right now, I have a simple scene with 2 players and a text field where I feed random numbers that updates every 2 seconds. The problem that I have is that the numbers are always different between client/server or server/client/client. My final application will have a server/client architecture, meaning none of the clients are going to be hosts.
I want the server to send the messages to each of the clients. In my current setup I have a script on my text prefab that changes the random number, the text prefab is also registered as a spawnable prefab in the network manager and it has a network identity component which doesn’t have the “Server” or “Local Player Authority” checkboxes on. I also have a empty object with a script and a network identity component with the “Server” flag on, that takes care of spawning the text prefab.
//This is the code that spawns the text prefab object
[SerializeField]
private GameObject counterPrefab;
public override void OnStartServer()
{
//Create prefab instance
GameObject counter = (GameObject)Instantiate(counterPrefab, transform.position, Quaternion.identity);
//Spawn
NetworkServer.Spawn(counter);
}
Given my setup I was expecting that using SyncVars would send the messages from the server and update correctly on each of the clients, but that wasn’t the case. I tried an Rcp call but it didn’t work either, I know that I’m doing something wrong. I’m hoping someone can guide me in the right direction.
//This is the code that is running on the text prefab
//to change the numbers and sync
[SerializeField]
private Text counter;
private string numbers;
[SyncVar]
private float randomNumber;
private float timer = 0;
void Update()
{
RpcCounter();
}
//I had an Rpc here but I removed it because it didn't work
void RpcCounter()
{
//Start counting
timer += Time.deltaTime;
if (timer >= 2)
{
randomNumber = Random.Range(1, 10);
numbers = randomNumber.ToString();
counter.text = numbers;
Debug.Log("Selected number:" + " " + numbers);
//genRandomNum(numbers);
timer = 0;
}
}
Thanks in advance!