Search Unity

Convert string to Int - C#

Discussion in 'Scripting' started by yoash, Jan 14, 2015.

  1. yoash

    yoash

    Joined:
    Sep 3, 2013
    Posts:
    36
    I have global.kills which is a string "10", but I want to convert it to an int called theKills. (When I check the type with Debug.Log, global.kills returns as a "Single")

    I've tried this: int theKills = int.Parse(global.kills);

    Which results in the following error: The best overloaded method match for `int.Parse(string)' has some invalid arguments

    My google links are all purple but I cannot find a way to convert this. Any help really appreciated
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,095
    are you 100% sure global.kills is a string?
     
    yoash likes this.
  3. yoash

    yoash

    Joined:
    Sep 3, 2013
    Posts:
    36
    Not 100% sure- when I try: Debug.Log (global.kills.GetType());

    The result is: System.Single

    On Googling though I couldn't find information on "Single". When I just Debug global.kills the result is a number (The number of kills the player has gotten).
     
  4. kdub

    kdub

    Joined:
    Apr 19, 2014
    Posts:
    165
    http://msdn.microsoft.com/en-us/library/ya5y69ds

    float == System.Single
    ±1.5e−45 to ±3.4e38
     
    yoash and Mycroft like this.
  5. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,095
    So its a float.
    if you really want it as int, use (int)global.kills
     
    yoash likes this.
  6. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    You might want to figure out why you got it as a float too, since you can't have a fraction of a kill, so why not int
     
    yoash and Mycroft like this.