Show Ping.time to text

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 ? :frowning:
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:

private Coroutine _pingTest;

...
if (_pingTest != null){
     StopCoroutine(_pingTest);
}

_pingTest = StartCoroutine(Pingtest());

...

Instead of

Invoke("Pingtest", 1f);

Thank you so much :smile:
that worked so well