我有一个预制件,上面有一个文本对象,该文本代表预制寿命的计数器,每秒钟都会增加。该脚本在人类与计算机模式下工作正常,但是当我尝试创建主机客户端时,客户端的数字总是不同的。如何让显示在它们上面的数字同步?
public class Timer : MonoBehaviour
{
public Planet pl;
private bool hasDone = false;
public int timeleft =6;
public Text countdownText;
// Use this for initialization
void Start()
{
timeleft = Random.Range (0, 31);
}
// Update is called once per frame
void Update()
{
countdownText.text = ("" + timeleft);
if (pl.get_owner () != null && hasDone == false) {
StartCoroutine("LoseTime");
hasDone = true;
}
if (pl.get_owner () == "Human") {
countdownText.color = new Color (0f, 0.5f, 1f, 1f);
}
if (pl.get_owner () == "Computer") {
countdownText.color = new Color (1f, 0.5f, 0f, 1f);
}
if (pl.get_collision () > 0) {
timeleft -= 1;
pl.decrease_collision ();
}
}
IEnumerator LoseTime()
{
if (pl.get_owner () != null) {
while (true)
{
yield return new WaitForSeconds(1);
timeleft++;
}
}
}
}