Search code examples
c#unity-game-enginechararray

Use variable instead of keycode unity


Okay so I'm trying to make a system that picks a random word then it turns that word into a char array. Then it will track if you type the characters. But the method that I'm trying to do hasn't been working. mainly because it won't let me use a variable name as a keycode. Is this a worthwhile problem, or should I abort mission and try something else.

string currentWord = wordArray[Random.Range(0, typingWords.Length)];
char[] wordAsArray = currentWord.ToCharArray();
Keycode currentLetter = wordAsArray[0];
if (Input.GetKey(currentLetter))
{
    Debug.Log("Test");
}

most of this works fine but what doesn't work the problem is the if (Input.GetKey(currentLetter))

is there something that can turn the word into a KeycodeArray or something like that or turn the specific character into keycode.

Does anybody know if this problem is solvable or will I have to use another method.


Solution

  • is there something that can turn the word into a KeycodeArray or something like that or turn the specific character into keycode.

    There is not a built-in function for that. You can create a keycode array with each keycodes in it and use that to determine current letter. Like:

    KeyCode[] letterCodes = new KeyCode[] {
        KeyCode.A, KeyCode.B, KeyCode.C, ... KeyCode.Z
    };
    KeyCode currentLetterCode = letterCodes[wordAsArray[0] - 'A'];
    

    'A' is subtracted from it to get the index of the letter in the letterCodes array. This assumes that the word only contains uppercase letters.

    Instead of that method i would use a different approach. I will write down a complete example of how i would do it with comments.

    public class TypingGame : MonoBehaviour
    {
        public Text wordText;
    
        private string currentWord;
        private int currentIndex;
    
        private void Start()
        {
            // Pick a random word to type
            currentWord = GetRandomWord();
            // Display the word on screen
            wordText.text = currentWord;
        }
    
        private void Update()
        {
            // Check if the current letter has been typed
            if (Input.anyKeyDown)
            {
                KeyCode keyPressed = GetKeyPressed();
                if (keyPressed != KeyCode.None && keyPressed == GetNextKeyCode())
                {
                    currentIndex++;
                    if (currentIndex >= currentWord.Length)
                    {
                        // The word has been completely typed
                        currentWord = GetRandomWord();
                        wordText.text = currentWord;
                        currentIndex = 0;
                    }
                    else
                    {
                        // Update the display to show the next letter
                        wordText.text = currentWord.Substring(currentIndex);
                    }
                }
            }
        }
    
        private string GetRandomWord()
        {
            // Replace this with your own word selection logic
            string[] words = { "cat", "dog", "bird", "fish" };
            return words[Random.Range(0, words.Length)];
        }
    
        private KeyCode GetNextKeyCode()
        {
            char nextChar = currentWord[currentIndex];
            KeyCode keyCode = (KeyCode)System.Enum.Parse(typeof(KeyCode), nextChar.ToString().ToUpper());
            return keyCode;
        }
    
        private KeyCode GetKeyPressed()
        {
            foreach (KeyCode keyCode in System.Enum.GetValues(typeof(KeyCode)))
            {
                if (Input.GetKeyDown(keyCode))
                {
                    return keyCode;
                }
            }
            return KeyCode.None;
        }
    }
    

    GetNextKeyCode method converts the next letter in the word to a KeyCode value by converting the letter to uppercase and using System.Enum.Parse to look up the corresponding KeyCode value.

    GetKeyPressed method checks which key was pressed and returns the corresponding KeyCode value by looping over all possible KeyCode values and checking which one has been pressed using Input.GetKeyDown.