Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

how to convert www.text to int

Discussion in 'Scripting' started by IllogicalGames, Mar 8, 2017.

  1. IllogicalGames

    IllogicalGames

    Joined:
    Apr 16, 2013
    Posts:
    167
    im trying to compare my app version with the current version using www object. it works fine on ios but giving me error in android. its weird as why this is not a problem in ios but returns error in android.

    my code:
    Code (CSharp):
    1. WWW Version = new WWW( "url link here");
    2. yield return Version;
    3. int pass = 1;
    4.         if(!string.IsNullOrEmpty(Version.error)) {
    5.             print( "Error downloading Version Number");
    6.             pass = 0;
    7.         }
    8.  
    9. if (pass == 1) {
    10.             if (CurrentVersion < int.Parse (Version.text)) {              
    11.                 init.InitGame ();          
    12.             }
     
  2. IllogicalGames

    IllogicalGames

    Joined:
    Apr 16, 2013
    Posts:
    167
    it gave me this error in editor

    Code (CSharp):
    1. FormatException: Input string was not in the correct format
    2. System.Int32.Parse (System.String s) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Int32.cs:629)
     
  3. GrowlerGrai

    GrowlerGrai

    Joined:
    Mar 8, 2017
    Posts:
    1
    Log your Version.text before the parse and check if the value is different on Android ?
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Use TryParse instead of Parse.
    Also, the 'pass' variable only clutters your code. Use if/else instead.
     
  5. Marceta

    Marceta

    Joined:
    Aug 5, 2013
    Posts:
    177
    Make sure your Version.text string is in correct format (no letters, symbols, etc).
    Also try this Convert.ToInt16(Version.text) or Convert.ToInt32(Version.text)

    using System;
     
  6. IllogicalGames

    IllogicalGames

    Joined:
    Apr 16, 2013
    Posts:
    167
    lol silly me, the url for android was wrong. that explains why i could run the script on ios. thanks anyway guys!
     
  7. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Nevertheless you should 1) check the WWW instance for errors and 2) use TryParse. ;)