Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Char in Javascript

Discussion in 'Scripting' started by Marble, Aug 23, 2006.

  1. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    This sounds silly, but how does one assign a value to a char in Javascript?

    This gives me a compile error:

    Code (csharp):
    1. var character : char;
    2.  
    3. character = 'g';
    The error is "unexpected char '''."
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Javascript doesn't have the concept of chars. Just use strings.
     
  3. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    Ok. I suppose if I had to use a .NET method that needed char input I could call a C# script.
     
  4. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    The usual solution I've seen is to use the index operator on a string:
    Code (csharp):
    1. var c : char;
    2. c = "s"[0];
    3.  
    4. // Or when calling a .Net method that needs a
    5. // char argument;
    6. SomeDotNetClass.MethodThatTakesAChar("s"[0]);
    7.  
     
    Mycroft likes this.
  5. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    Neat technique. Thank you.
     
  6. drudiverse

    drudiverse

    Joined:
    May 16, 2013
    Posts:
    218
    Something has been updated regarding this question, JS has char now:


    function findMatchingParenthesis( str : String, openPos : int ) : int
    {
    var closePos :int = openPos;
    var counter :int = 1;
    while (counter > 0)
    {
    var c:char = str[++closePos];
    if (c == '(') { counter++; }
    else if (c == ')') { counter--; }
    }
    return closePos;
    }

    find matching parenthesis bracket in unity3d