- Home /
Pulling the first two characters from a string. Code not working.
WWW cityRequest = new WWW("http://www.geoplugin.net/json.gp?ip="+ currentIP); //get player location
yield return cityRequest;
var N = JSON.Parse(cityRequest.text);
currentCity = N["geoplugin_city"].Value;
currentCountry = N["geoplugin_countryName"].Value;
currentLon = N["geoplugin_latitude"].Value;
currentLat = N["geoplugin_longitude"].Value;
string shortLon = currentLon.Substring(0,2);
string shortLat = currentLat.Substring(0,2);
It looks like it would work and from what I google it should but when I actually put it into practice but when I run the program I get nothing.
currentLon: 23.43924*
currentLat: 58.23423
shortLon:
shortLan:
*do note this just random numbers and not the actual numbers that was generated by my program.
EDIT: Added image that shows issue.
No that is why I am confused, I don't get any errors it just doesn't give a value when running it on unity. I'll try to get a picture so it'll be more clear.
Okay I feel really silly relative to how much work I put into solving it.
The solution is there is no problem. It just doesn't show up on the inspector for some reason but if I actually draw it onto the game screen it shows up.
Despite all my messing around in the code, I have no idea why I never tried that.
Answer by hangemhigh · Apr 24, 2015 at 08:25 PM
Maybe WWW is not done before you accessed it. Try this
WWW cityRequest;
// Use this for initialization
void Start ()
{
StartCoroutine(getCityInfo());
}
// Update is called once per frame
void Update ()
{
if(cityRequest.isDone){
showResult();
}
}
IEnumerator getCityInfo(){
cityRequest = new WWW ("http://www.geoplugin.net/json.gp?ip=" + currentIP); //get player location
//Wait until the info is done receing bytes
while(!cityRequest.isDone){
yield return null;
}
}
void showResult(){
if(cityRequest != null && cityRequest.isDone){
var N = JSON.Parse (cityRequest.text);
currentCity = N ["geoplugin_city"].Value;
currentCountry = N ["geoplugin_countryName"].Value;
currentLon = N ["geoplugin_latitude"].Value;
currentLat = N ["geoplugin_longitude"].Value;
string shortLon = currentLon.Substring (0, 2);
string shortLat = currentLat.Substring (0, 2);
}
}
Or maybe you have trailing characters in the string. Use shortLon.Trim() and shortLat.Trim() to remove the leading and trailing white space then try the Substring function again.
Your answer
Follow this Question
Related Questions
c# convert int to string 2 Answers
Converting String to a Game Object 1 Answer
Convert string to float or double - errors 1 Answer
Convert GameObject to String? 2 Answers
Array - Convert Object into Int 5 Answers