2

How can I check internet connection inside Update Function in unity? I want to know whether the user is connected or not, and based on that just disable some functionality of my game.

Seems like there is no question like this been asked here before or if it is, it just checking the connection in Start function. Here is the code I have so far:

void Update () {

    if (Application.internetReachability == NetworkReachability.NotReachable) {
        Debug.Log ("No internet access");
    } else {
        Debug.Log ("internet connection");
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
amdev
  • 6,703
  • 6
  • 42
  • 64
  • 1
    Possible duplicate of [Check for internet connectivity from Unity](https://stackoverflow.com/questions/34138707/check-for-internet-connectivity-from-unity) – mjwills Aug 12 '17 at 06:50
  • 1
    thanx man i already using that functions that been provided in those link in my Update function but the problem is that it like start function just checks for connection one time , and while i hit play button in unity editor and after a few seconds i just disabling my computer from internet access i just still returns the same result . – amdev Aug 12 '17 at 06:54
  • 1
    i just turned my xampp off and it just causes a silly shamed errors , any way NO , because my connection is on in my computer i use xampp for db and hopefully it shows the result of second Debug.Log("internet connection") , but the problem is when game ruining and i turn my internet off it STILL shows the same result , how can i solve this ??? – amdev Aug 12 '17 at 07:05
  • 1
    simple -> , there is internet connection , i hit play button , it shows internet connection in log , NOW i turn off internet connection from computer , it STILL Shows the same thing – amdev Aug 12 '17 at 07:11
  • 1
    So, let's think this through. You run the code, and it detects the internet is there. Then you disconnect the internet and don't run the function again. How are you expecting it to detect the disconnected internet **without** running the function again? – mjwills Aug 12 '17 at 07:13
  • what is the Update function says in unity documentation >>> IT WILL RUN AUTOMATICALLY EVERY SINGLE FRAME (OK ?) so it should run it every frame , means that that if else in inside update should run every single frame , but it does not run , (by the way tanx for helping me through this , i know that this is going too much being silly :) – amdev Aug 12 '17 at 07:17
  • Can you update your post to include the entire class? It sounds like your real problem is _why does `Update` only run once_? – mjwills Aug 12 '17 at 07:19
  • Does http://answers.unity3d.com/questions/994964/update-is-only-called-once.html help? Or http://answers.unity3d.com/questions/312400/update-function-only-fires-once.html ? – mjwills Aug 12 '17 at 07:21
  • if your game is 60fps, update called 60 times in second. so you don't need to change it in this time, so you need to do this periodically. – BlackMB Aug 12 '17 at 08:33
  • how can i do that ? – amdev Aug 12 '17 at 10:25
  • @Er.Ellison My below code does that for you. 1f means 'check this very second` (see https://docs.unity3d.com/ScriptReference/Time-deltaTime.html). If you want it to be every minute, change it to 60f. – mjwills Aug 12 '17 at 11:01

3 Answers3

4

@mjwills solution is right and it works OK, but nobody forces you to check connection every single frame of your game. You don't have to do that.

A better solution is to check connection when a button is clicked or when an event happens in your scene.

halfer
  • 19,824
  • 17
  • 99
  • 186
amdev
  • 6,703
  • 6
  • 42
  • 64
2

You need to run your Update function periodically.

http://unitylore.com/articles/timers-in-unity/ could be used for this:

using UnityEngine;

public class Timer : MonoBehaviour 
{
    public float waitTime = 1f;

    float timer;

    void Update () 
    {
        timer += Time.deltaTime;
        if (timer > waitTime) { 
            if (Application.internetReachability == NetworkReachability.NotReachable) {
                Debug.Log ("No internet access");
            } else {
                Debug.Log ("internet connection");
            }
            timer = 0f;
        }
    }
}
mjwills
  • 23,389
  • 6
  • 40
  • 63
2

Unity's Application.internetReachability is not the best solution for internet detection as it was not designed for that purpose (as stated in the docs).

The proper way is to implement a technique called Captive Portal Detection, which is what all the major OS's use for their internet status detection. If implemented correctly, it can even detect when the network is restricted (hotels or airports) as it relies on HTTP requests of known content. Therefore it is far more reliable.

It is not that hard to implement. You need to make an HTTP request to a known "check page", and check whether the right content were returned.

However, if you want a complete, ready-to-use solution, you can check the asset Eazy NetChecker which I created for this purpose. It also has events and a custom editor. It is not free, but it is super cheap!

halfer
  • 19,824
  • 17
  • 99
  • 186
JackMini36
  • 621
  • 1
  • 5
  • 14