I'm trying to create a falling word typing game like z-type.

I have used the code provided here — but it's is only for keyboards:

public class WordInput : MonoBehaviour {

    public WordManager wordManager;

    void Update () {

        foreach (char letter in Input.inputString)
        {
            wordManager.TypeLetter(letter);   
        }    
    }
}

I want to implement this on mobiles as well. So I have created a mobile keypad using a canvas and buttons for alphabets but I'm unable to link it to words shown on the screen.

It's reading letters from Input.inputString. So all we have to do is provide our own alternate input source.

public class CustomInput : MonoBehaviour {

    string inputThisFrame = "";

    // Call this method from your on-screen buttons.
    public void Type(string text) {
        inputThisFrame += text;
    }

    // Expose text the same way Input does.
    public string inputString {
        get {
             if(inputThisFrame != "")
                 return inputThisFrame;

             // Fall back on built-in input.
             return Input.inputString;
        }
    }

    // At the end of the frame, clear the buffer
    // so we start from scratch next frame.
    void LateUpdate() {
        inputThisFrame = "";
    }
}

Now you can:

  1. Place an instance of CustomInput in your scene
  2. Wire up your on-screen buttons to report their typing to the CustomInput instance
  3. Modify WordInput so it takes an instance of CustomInput and asks for its inputString instead of the built-in Input.

    The fallback we added above ensures the regular keyboard still works, when presentz which helps with testing.

  • Thanks for the response @DMGregory ...I'm still having issues with the keypad. I have created the CustomInput script and added it to the main camera. Then I added the CustomInput script to the button's "On Click" function...Modified the wordinput script as follows : foreach(char letter in customInput.inputString) { wordManager.TypeLetter(letter); } But get a null reference error to the foreach line....Any advise on how to rectify this? – single arrow games Nov 30 at 14:03
  • Did you remember to assign WordInput's customInput instance, or did you leave it as null? – DMGregory Nov 30 at 14:30
  • no @DMGregory, I haven't added...not sure how to do that..this what I have:public WordManager wordManager; public CustomInput customInput; // Update is called once per frame void Update () { foreach(char letter in customInput.inputString) { wordManager.TypeLetter(letter); } } – single arrow games Nov 30 at 14:33
  • Assign the customInput reference in the Inspector. – DMGregory Nov 30 at 14:37
  • it works fine now...thanks a lot for the help – single arrow games Nov 30 at 14:46

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.