Need help in typing project....!

Hi,
Am doing a typing game project , its for kids, in a google search i find one reference code its exactly match what i want, then i modify the code what i want it to.

Here the problem is , in a word “APPLE” the “P” letter is twice so my the code finds multiple “P” letter at a time,
i want it to find or check one letter and in a sequence, please give me some help in this to achieve this.

Below is the Code C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class LetterButton : MonoBehaviour
{
    public Button[] aLetter;
    private GameObject centre;
    public RectTransform gameCharContainer;
    public GameObject alphabetLetter;

    private string wordToType = "";
    private int lengthOfWordToType;
    char [] lettersToType;
    public bool [] lettersTyped;

    private string [] wordsToType = new string [] {"apple", "ball","cat","dog","elephant"};

    void Start ()
    {
        centre = GameObject.Find ("Centre Text GO");


        initGame ();

        GamePlayFunction ();

        IntLettersPressed ();
    }

    void initGame()
    {

        int numberOfWords = Random.Range(0,wordsToType.Length-1);

        wordToType = wordsToType [numberOfWords];

        lengthOfWordToType = wordToType.Length;

        wordToType = wordToType.ToUpper ();

        lettersToType = new char[lengthOfWordToType];

        lettersTyped = new bool [lengthOfWordToType];

        lettersToType = wordToType.ToCharArray ();

    }

    void GamePlayFunction()
    {
        int nbletters = lengthOfWordToType;

        for (int i = 0; i < nbletters; i++) {
            Vector3 newPosition;
            newPosition = new Vector3 (centre.transform.position.x + ((i-nbletters/2.0f) *70), centre.transform.position.y, centre.transform.position.z);
            GameObject l = (GameObject)Instantiate (alphabetLetter, newPosition, Quaternion.identity);
            l.name = "letter" + (i + 1);
            l.GetComponent<Text> ().text += wordToType[i];
            l.transform.SetParent (gameCharContainer, false);
        }
    }

    void IntLettersPressed()
    {
        for (int i = 0; i < aLetter.Length; i++) {
            Text a = aLetter[i].transform.FindChild("Text").GetComponent<Text>();
            aLetter[i].GetComponent<Button> ().onClick.AddListener (() => PressedBtn (a.text));
        }
    }

    void PressedBtn(string pressedLetter)//BY PRESSING THE GAME KEYBOARD BUTTONS
    {
        char letterTyped = pressedLetter.ToCharArray () [0];
        for (int i = 0; i < lengthOfWordToType; i++)
        {
            if (!lettersTyped [i])
            {
                letterTyped = System.Char.ToUpper (letterTyped);
                if (lettersToType [i] == letterTyped)
                {
                    lettersTyped [i] = true;
                    GameObject.Find ("letter" + (i+1)).GetComponent<Text> ().color = new Color (0, 0, 0);
                }
            }

        }
    }
}

I would suggest two solutions. Solution one is to loop through the length of the word that is currently entered and the full word to see if all the letters match to that point.

So if the word is apple but I have typed app so far, it’s still a match. I loop through with the typed word being 3 for it’s length and compare the index of each letter in the two strings.

The other option is to see what char index I need to compare. So again, if my word is apple and I type in a, then the length of the word I typed in is 1, so I need to compare index 0. Then when I type in p. I compare index 1 of each string. Then the second p and I compare index 2 of each word. And so forth. The win condition is just when all letters match and then lengths match as well.

Since your words aren’t that big, either method should work fine.

Thx, am doing the 2nd method as it so far.
can you plz give me some simple code? as you mentioned the 2nd method?
coz am not well in coding, but i can try it.

When a latter is typed, you could do something like this

    private void CheckForMatch()
    {
        if(typedLetters[typedLetters.Length - 1] == wordToMatch[typedLetters.Length - 1])
        {
            //We have a matched letter
            //Now check if typedLetters.Length == wordToMatch.Length to see if all letters are typed
            //If so, display win, if not, player can keep typing
        }
        else
        {
            //Letter doesn't match, display proper error.
        }
    }

This is just a basic idea. A letter is typed in, you add it to your typedLetters string (or if you have a text object, add it to that and pull the Text.text value) then call this method to do your compare.

Hi, this is not working.
It gives total length of words minus one.
If in a word BALL , the total length is 4 , it gives 3 as a result.

It returns the letter in the index. Arrays are 0 based, so of course it returns 3. You want index 3 if you’re looking for the last letter in a word. So when you type a letter. Your very first one is in index 0 not index 1.

I’m not sure what you are trying to get at. If you attempted something, then show your new code. Otherwise, what I showed should work right.

1 Like
private void CheckForMatch(string pressedLetters){
for(int i = 0;i<pressedLetters.length-1;i++){
if(wordToMatch[I][I][I][I] == pressedLetters[I][I][I][I]){
//All good maybe highlight the letter to show its ok
}
else{
//display error message
}
}
}

[/I][/I][/I][/I][/I][/I][/I][/I]

I want exactly like this…

https://play.google.com/store/apps/details?id=com.husseinelfeky.typingmaster&hl=en

thanks for code lines,
I will soon check your code…

Off by one error in the for loop, should be <=. And what is wordToMatch followed by the 4 sets of brackets? It looks like a 4 dimensional array with an undeclared variable. Perhaps syntax I’m unfamiliar with, or markdown maybe.

ya exactly it shows an below error

//error CS0021: Cannot apply indexing with [ ] to an expression of type `char’

Any how am attaching a project , please check what i did, i want letters to type in series…

Thanks a lot being with me

3163995–240802–Kids Play.zip (2.07 MB)

What is the best method , to play sound/audio of letter?
What i mean is, if i type letter “A” i want to play that alphabet sound/audio.
How can i do this Process? Is there any plugin there?

You mean in English? Any language should work, but you would likely need to record the audio clips yourself which shouldn’t be too hard. Otherwise a Google search brings up:

http://www.theblog.ca/mp3-audio-files-alphabet

To play the audio in Unity, this may help:

Remember to search the forums and use Google before posting.

Can u give me simple code line that how i can play certain letter audio/sound??

Yep. Located here.
https://docs.unity3d.com/ScriptReference/AudioSource.PlayOneShot.html
https://docs.unity3d.com/ScriptReference/AudioSource.Play.html

Honestly, as suggested, use Google. A lot of what you want has answers in the Script Reference that Unity put out.

Thanks Let me try this…

Generally, asking for code is not the way to go. If you require code to be sent to you to create concepts that have already been explained in quite a detailed way, then you need to be doing tutorials first before trying to jump right ahead to making your own game.

Think of it this way; what’s the point of making a game if you cant create it on your own?

https://unity3d.com/learn for more info