How to check internet connection in Unity

Hello programmers, In this article, I will show how to check the internet connection in Unity.

Before we get started with the building process, we need to know a few concepts. Let’s first discuss them one by one.

Application.internetReachability

This returns the type of internet connectivity that is currently possible on the device.

This property is primarily useful to distinguish good or bad internet connections from carrier networking.

Syntax:-

Application.internetReachability

NetworkReachability.NotReachable

This property is used to check whether the network is not reachable.

Syntax:-

NetworkReachability.NotReachable

 

The below-given program is to check internet connection in Unity :-

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CheckConnection : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        if (Application.internetReachability == NetworkReachability.NotReachable)
        {
            Debug.Log("Error. Please Check your internet connection!");
        }
        else
        {
            Debug.Log("Connected to Network");
        }
    }

}

Output :-

Internet Connection is ON :-

How to check internet connection in Unity

Internet Connection is OFF :-

How to check internet connection in Unity

Leave a Reply

Your email address will not be published.