- Home /
Sending a variable from the server to a client
Hello dear community. I am trying to implement a simple idea: if a player connects to a server, he/she gets notified how long the server has been up.
I produced some code and tried to test it, but it doesn't work: players see wrong uptime (done in colour rendering). In my code a player, connected to server sees his colour change (if uptime gets more than a certain value), but new players, who have connected afterwards see the uptime start from the beginning and don't percieve the colour of other players. Network view component has been attached to both player prefab and server object.
I am supplying 2 scripts below and asking for help. Could you please tell me how to properly send the uptime variable to a newly connected player, so he/she can use it? Thank you!
////////// SERVER.CS //////////
using UnityEngine;
using System.Collections;
public class server : MonoBehaviour {
public int i;
public void Start()
{
}
void OnServerInitialized(){
StartCoroutine (Uptimecounter ());
}
void OnPlayerConnected(NetworkPlayer player) {
Debug.Log("Player connected");
}
[RPC]
IEnumerator Uptimecounter(){
for (i = 0; i < 100; i++){
yield return new WaitForSeconds(1);
// Debug.Log (i);
}
}
}
////////////// ONCONNECT.CS //////////////
using UnityEngine;
using System.Collections;
public class onconnect : MonoBehaviour {
public server link;
void Start () {
link = GameObject.Find("NetworkManager").GetComponent<server>();
Debug.Log (link.i);
}
void Update () {
if (link.i < 5) {
renderer.material.color = Color.red;
}
if ((link.i > 5)&&(link.i<10)) {
renderer.material.color = Color.blue;
}
if ((link.i > 10)&&(link.i<15)) {
renderer.material.color = Color.green;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613164612im_/https://answers.unity.com/themes/thub/images/avi.jpg)