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" argument to function called by Button OnClick()

Discussion in 'UGUI & TextMesh Pro' started by robert-sherwood, Feb 19, 2021.

  1. robert-sherwood

    robert-sherwood

    Joined:
    Jan 7, 2018
    Posts:
    14
    Hey, all. I am writing a game, with a scene that emulates a keyboard. I am defining a button for every key*, and calling a function in the target GameObject, passing in an argument defining which 'key' was pressed. Since the keyboard will only generate one character per press, I defined a method called "punchChar" that will take the input and take some action based on the character entered.

    The function signature was:

    public void PunchChar(char key)

    {
    // Stuff goes here
    }

    Note the function took a "char" input. The method was not visible to the button onClick() in the Unity UI until I changed the signature to this:

    public void PunchChar(string key)

    {
    // Stuff goes here
    }

    Everything is working now, so it's not an emergency, but does anyone know why "char" is not an allowed argument to a function called by <Button>.onClick()? Seems like a really arbitrary restriction but there may be a great reason I am not aware of.

    *In addition to keyboard mapping, I want to use touch controls, which is why I am creating buttons instead of just using keyboard input.