Question by
unity_WIlIzj9wXMU5Iw · May 18, 2018 at 09:10 AM ·
unity 5networkingtextcanvas
canvas/text object sync
I have a prefab that has a text object on it ,And that text represent a counter for the prefab lifespan that increases every second ,The script works fine on the human vs computer mode ,But when i try to make a host / client the numbers are always 0 (Tried SyncVar Didn't work).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class Timer : NetworkBehaviour
{
private bool hasDone = false;
public Planet pl;
public Text countdownText;
[SyncVar]
private int timeleft;
void Start()
{
if(isLocalPlayer)
timeleft = Random.Range(30, 50);
}
// Update is called once per frame
void Update()
{
countdownText.text = ("" + get_t());
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);
}
}
IEnumerator LoseTime()
{
if (pl.get_owner() != null) {
while (true)
{
yield return new WaitForSeconds(2);
set_t(get_t()+1);
}
}
}
public void set_t(int a)
{
timeleft = a;
}
public int get_t()
{
return timeleft;
}
}
Comment