1

I am tried to obtaining IP address using unity but I am unable to find it anywhere. If it is founded then nothing seems to be worked.

https://docs.unity3d.com/ScriptReference/LocationService.Start.html

This is Unity official code which is not working at my end .It shows nothing .Similarly all types of codes not worked up.

derHugo
  • 83,094
  • 9
  • 75
  • 115
  • Do I get it right, you want to find out the IP Adress? – cpaech Apr 04 '22 at 13:52
  • yeah I want find my Ip address also another user Ip adress. Idk how to apply or I also worked with API like Json but they are also not working in unity. – Pankaj Bansal Apr 04 '22 at 15:24
  • Does this answer your question?, Note you need Internetacces. https://stackoverflow.com/questions/3253701/get-public-external-ip-address – cpaech Apr 04 '22 at 17:08
  • Please use the correct tags! Note that [`[unityscript]`](https://stackoverflow.com/tags/unityscript/info) is or better **was** a custom JavaScript flavor-like language used in early Unity versions and is **long deprecated** by now. Pretty sure you are not asking about that language – derHugo Apr 06 '22 at 08:33

1 Answers1

1

Do you want to know IP address or Location? I don't know how can you get the user's IP address but there is a straightforward method for finding the user's location using GPS. If you want to do this, this youtube video will be useful for you ^^

Edit:

You can try this code that I found on this link. (Also check the linkie for more details)

public void GetUserLocation()
        {
            if( !Input.location.isEnabledByUser ) //FIRST IM CHACKING FOR PERMISSION IF "true" IT MEANS USER GAVED PERMISSION FOR USING LOCATION INFORMATION
            {
                statusTxt.text = "No Permission";
                Permission.RequestUserPermission(Permission.FineLocation);
            }
            else
            {
                statusTxt.text = "Ok Permission";
                StartCoroutine("GetLatLonUsingGPS");
            }
        }
 
        IEnumerator GetLatLonUsingGPS()
        {
            Input.location.Start();
            int maxWait = 5;
            while( Input.location.status == LocationServiceStatus.Initializing && maxWait > 0 )
            {
                yield return new WaitForSeconds(1);
                maxWait--;
            }
            if( maxWait < 1 )
            {
                statusTxt.text = "Failed To Iniyilize in 10 seconds";
                yield break;
            }
            if( Input.location.status == LocationServiceStatus.Failed )
            {
                statusTxt.text = "Failed To Initialize";
                yield break;
            }
            else
            {
                statusTxt.text ="waiting before getting lat and lon";
                // yield return new WaitForSeconds(5);
                // Access granted and location value could be retrieve
                double longitude = Input.location.lastData.longitude;
                double latitude = Input.location.lastData.latitude;
 
                AddLocation(latitude, longitude);
                statusTxt.text="" + Input.location.status + "  lat:"+latitude+"  long:"+longitude;
            }
            //Stop retrieving location
            Input.location.Stop();
            StopCoroutine("Start");
        }

EDIT - 2:

For some reason, This code is not working and statusTxt is stuck on the "Failed To Iniyilize in 10 seconds" text. However, this problem is easy to solve! Just make it more straightforward! Remove all the if's in GetLatLonUsingGPS() method. My code was like that:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Android;
using UnityEngine.UI;

public class GetLocations : MonoBehaviour
{
    public Text statusTxt;

    void Update()
    {
        GetUserLocation();
    }


    public void GetUserLocation()
    {
        if (!Input.location.isEnabledByUser) //FIRST IM CHACKING FOR PERMISSION IF "true" IT MEANS USER GAVED PERMISSION FOR USING LOCATION INFORMATION
        {
            statusTxt.text = "No Permission";
            Permission.RequestUserPermission(Permission.FineLocation);
        }
        else
        {
            statusTxt.text = "Permission Granted";
            StartCoroutine(GetLatLonUsingGPS());
        }
    }

    IEnumerator GetLatLonUsingGPS()
    {
        Input.location.Start();
        int maxWait = 5;
        while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
        {
            yield return new WaitForSeconds(1);
            maxWait--;
        }

        statusTxt.text = "waiting before getting lat and lon";
        
        // Access granted and location value could be retrieve
        double longitude = Input.location.lastData.longitude;
        double latitude = Input.location.lastData.latitude;
        
        //AddLocation(latitude, longitude);
        statusTxt.text = "" + Input.location.status + "  lat:" + latitude + "  long:" + longitude;

        //Stop retrieving location
        Input.location.Stop();
        StopCoroutine("Start");
    }
}

There is only one problem in this code; don't call the GetUserLocation() method on the Update() method without any control bool or timer. Otherwise, StatusTxt will continuously change. For example, you can call it every 5 seconds.