Hi. I'm trying to show the ping result in screen text as the title says. - I need it to refresh every 1 sec. - I need the to break the method and restart it if the button is clicked again. this is my code, and it's not working. anyone can help me please ? Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine.UI; using UnityEngine; using UnityEngine.Networking; public class TestPing : MonoBehaviour { public Text txt; void Start(){ txt = GetComponent <Text> (); } public void onClick(){ Invoke ("Pingtest", 1f); } IEnumerator Pingtest(){ while (true) { var Pingtest = new Ping ("104.160.141.3"); while (!Pingtest.isDone) { yield return null; } yield return new WaitForSeconds (1); txt.text = (Pingtest.time).ToString(); } } }
You're executing your coroutine instead of starting it. Use: Code (CSharp): private Coroutine _pingTest; ... if (_pingTest != null){ StopCoroutine(_pingTest); } _pingTest = StartCoroutine(Pingtest()); ... Instead of Code (CSharp): Invoke("Pingtest", 1f);