Unity Community


Page 1 of 2 12 LastLast
Results 1 to 20 of 33

  1. Location
    Vancouver, WA
    Posts
    295

    String to int...

    I am having some difficulties getting a string converted to an int in JavaScript. Say I have some code like this:

    Code:  
    1. var numberString "12345";
    2. var numberInt;
    3. numberInt = int.Parse(numberString);
    4. numberInt++;
    5. // numberInt now is 12346

    Thats what should happen. I converted the C# code from here to that JS. Problem is, it seems like it doesn't know that numberString is the right type of string or something, because it outputs the error "System.FormatException: Invalid format" on the int.Parse() line.


  2. Location
    Unity HQ, Denmark
    Posts
    1,148
    I assume that the missing = operator on line 1 is just a transcription error.... :P

    For some reason int.Parse (Or rather System.Int32.Parse) is defined to return System.Object instead of an int.

    If you explicitly define the type of numberInt, it should work:
    Code:  
    1. var numberInt: int;

    ... although the InvalidFormat exception is raised when you give Parse a string that can't be parsed as a number, so I have a suspicion that you already were that far.
    -Keli

    If you live in a bamboo house, don't throw with pandas.


  3. Location
    Zürich, CH
    Posts
    56
    I'm trying to go the other way around.
    Code:  
    1. var currentInt = 0;
    2. var currentStr = "0";
    3.  
    4. if (currentInt != 10){
    5.     currentInt++;
    6.     currentStr = currentInt.toString;
    7. }

    I get the error: 'toString' is not a member of 'System.Int32'

    Is this the proper way to do it?


  4. Location
    Unity Technologies HQ, Copenhagen
    Posts
    3,514
    currentInt.ToString();
    Joachim Ante


  5. Location
    Zürich, CH
    Posts
    56
    Thank you Joachim, but this is giving me some trouble in my array.
    Code:  
    1. var currentInt = 0;
    2. var currentStr = "0";
    3.  
    4. function Update () {
    5.  
    6.     if (currentInt != 10){
    7.         var node = Array();
    8.         node.Add(currentInt);
    9.         node[currentInt] = GameObject.Find (currentStr);
    10.         currentInt ++;
    11.         currentStr = currentInt.ToString();
    12.     }
    13. }
    It appears the original variable goes through nicely, but once i add one and try to go through a second time it gives me "Array index is out of range."

    Is this properly converting into a string? If yes, did I miss something else?


  6. Location
    Unity Technologies HQ, Copenhagen
    Posts
    3,514
    You are recreating the array from scratch every frame. Thus it will always start out zero size then you resize it to contain 1 element.
    Joachim Ante


  7. Location
    Zürich, CH
    Posts
    56
    DOH! Thank you once again.


  8. Location
    Alhambra, California - US
    Posts
    780
    okay... um... so how do you convert a string like "23" to the Int 23? Seems like this thread only went the other way around.


  9. Location
    Sacramento CA, USA
    Posts
    1,590
    Quote Originally Posted by deram_scholzara
    okay... um... so how do you convert a string like "23" to the Int 23? Seems like this thread only went the other way around.
    Try this:

    Code:  
    1. var myString : String = "23";
    2.  
    3. function Start ()
    4. {
    5.      var theInt : int = parseInt (myString);
    6. }


  10. Location
    Alhambra, California - US
    Posts
    780
    Edit: oops! nevermind... just forgot the "var" at the beginning of line 8.


    Hmm... I just tried it in my script:
    Code:  
    1. var widthFeet = "25";
    2. //var heightFeet = "10";
    3. //var depthFeet = "30";
    4.  
    5.  
    6. function OnGUI () {
    7.     var tempWidth = GUI.TextField (Rect (10, 10, 200, 20), widthFeet, 2);
    8.     tempWidthInt : int = parseInt (tempWidth);
    9.     if (tempWidthInt > 0 && tempWidthInt <= 36) {
    10.         widthFeet = tempWidth;
    11.     }
    12.  
    13. }

    and I get:
    RoomSizeInput.js(8) error BCE0043: Unexpected token: :.
    and
    RoomSizeInput.js(8) error UCE0001: ';' expected. Insert a semicolon at the end.


  11. Location
    Las Vegas
    Posts
    3
    Since I was wondering the same thing in C#...

    In C#, the code is:
    Code:  
    1. int intSix = 6;
    2. string strSix = intSix.ToString();


  12. Posts
    293
    Quote Originally Posted by deram_scholzara
    Edit: oops! nevermind... just forgot the "var" at the beginning of line 8.


    Hmm... I just tried it in my script:
    Code:  
    1. var widthFeet = "25";
    2. //var heightFeet = "10";
    3. //var depthFeet = "30";
    4.  
    5.  
    6. function OnGUI () {
    7.     var tempWidth = GUI.TextField (Rect (10, 10, 200, 20), widthFeet, 2);
    8.     tempWidthInt : int = parseInt (tempWidth);
    9.     if (tempWidthInt > 0 && tempWidthInt <= 36) {
    10.         widthFeet = tempWidth;
    11.     }
    12.  
    13. }

    and I get:
    RoomSizeInput.js( error BCE0043: Unexpected token: :.
    and
    RoomSizeInput.js( error UCE0001: ';' expected. Insert a semicolon at the end.
    You forgot the weord "var":

    var tempWidthInt : int = parseInt (tempWidth);

    Edit:

    Didn't see your edit

    Edit 2:

    I did not like the above ways of converting between int and float. I was unable to get int.TryParse(string,int) to work, System.Int32.TryParse(string,int) did not work, and while parseInt (which is completely undocumented from what I can tell) failed on strings which contained characters.

    This is the method I used:

    Code:  
    1.         try
    2.         {
    3.         var myInt : int = System.Convert.ToInt32(levelToLoad);
    4.         }
    5.         catch (err)
    6.         {
    7.         //code to handle non-numeric strings here
    8.         }

    For new comers, check out the .Net Framework documentation on System.Convert.

  13. Volunteer Moderator
    Posts
    23,693
    Quote Originally Posted by iceshaft07
    [I was unable to get int.TryParse(string,int) to work, System.Int32.TryParse(string,int) did not work
    Those are the same thing. Also, it works fine:

    Code:  
    1. var aString = "123456";
    2. var stringVal = 0;
    3. if (!int.TryParse(aString, stringVal))
    4.     Debug.Log ("Numbers only!");
    5. else
    6.     Debug.Log (stringVal);

    --Eric
    SpriteTile: new tile system that works seamlessly with Unity 4.3 sprites
    FlyingText3D: dynamic 3D text with TTF fonts | Vectrosity: fast & easy line drawing
    Nifty utilities! Stitch terrains together - runtime model importing - file browser - fractal landscapes

  14. Pls help me to convert string to Int (C#);
    such as : "23" >>> 23;

    i can't use :" int.Parse("23") "


  15. Location
    Toronto, Ontario, Canada
    Posts
    1,313
    You should be able to use int.Parse("23"), perhaps you wrote it wrong in your code?

    Try this instead:
    int number = System.Int32.Parse("23");

  16. Thanks FizixMan very much.


  17. Posts
    89
    The reasons why they dont work is because you guys dont declare the variables.. i see var i = "stuff" and things like that.. it works better if you type var i:String = "stuff"; it's also less intensive for unity and it's easier to see whats what...

  18. Volunteer Moderator
    Posts
    23,693
    Quote Originally Posted by Themperror View Post
    The reasons why they dont work is because you guys dont declare the variables.. i see var i = "stuff" and things like that.. it works better if you type var i:String = "stuff"; it's also less intensive for unity and it's easier to see whats what...
    That's completely wrong. var i = "stuff" is exactly the same as var i : String = "stuff". It doesn't work better or worse either way, nor is it any more or less intensive either way. It's called "type inference". I would also disagree that it's any easier to see what's what, at least in this case, since putting something in quotes makes it very obvious that it's a string, especially with syntax coloring.

    --Eric
    SpriteTile: new tile system that works seamlessly with Unity 4.3 sprites
    FlyingText3D: dynamic 3D text with TTF fonts | Vectrosity: fast & easy line drawing
    Nifty utilities! Stitch terrains together - runtime model importing - file browser - fractal landscapes


  19. Posts
    1
    Quote Originally Posted by Eric5h5 View Post
    That's completely wrong. var i = "stuff" is exactly the same as var i : String = "stuff". It doesn't work better or worse either way, nor is it any more or less intensive either way. It's called "type inference". I would also disagree that it's any easier to see what's what, at least in this case, since putting something in quotes makes it very obvious that it's a string, especially with syntax coloring.

    --Eric
    That's what everyone thinks. Then they get some programming experience and learn how to actually program efficiently. You shouldn't reply to programming questions based entirely on what you think is right. Programming requires perfect precision knowledge, and does not work well on feelings. Atleast not until someone makes a proper AI of some sort...

    While you are correct that the end result is essentially the same between var i = "stuff", and var i:String = "Stuff", that will not hold true once your project turns resourcedemanding enough for you to want to savor every little bit of cpu-cycle you can on every nook and cranny to gain better fps.
    var i:String = "Stuff"; // this one is Ever so slightly faster, and is in fact part of the optimization process you should do on every project.

    Sure, simple projects won't need as much attention to optimizations as larger ones, but it's a good programming habit to always declare your types. Even when it's not needed.


  20. Location
    USA
    Posts
    2,535
    Quote Originally Posted by uberubert View Post
    That's what everyone thinks. Then they get some programming experience and learn how to actually program efficiently. You shouldn't reply to programming questions based entirely on what you think is right. Programming requires perfect precision knowledge, and does not work well on feelings. Atleast not until someone makes a proper AI of some sort...

    While you are correct that the end result is essentially the same between var i = "stuff", and var i:String = "Stuff", that will not hold true once your project turns resourcedemanding enough for you to want to savor every little bit of cpu-cycle you can on every nook and cranny to gain better fps.
    var i:String = "Stuff"; // this one is Ever so slightly faster, and is in fact part of the optimization process you should do on every project.

    Sure, simple projects won't need as much attention to optimizations as larger ones, but it's a good programming habit to always declare your types. Even when it's not needed.
    I was always under the impression that type inference only cost you in compile time and not run time performance.

    Yay replying to a thread from 2005!
    Fractured State - A moddable RTS game: Dev Blog | Facebook | Twitter | Youtube | ModDB

    Personal Twitter

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •