Typing Game script

Hi I am trying to make a typing game that is based on comparing strings and this is the script for the first word "The". Everything works except that when I use Event.Current.isKey and display it on my string it wont let me use the backspace to delete the letter already typed. Please Help.

var theString : String = "";
var theSkin : GUISkin;
var The : GameObject;

function OnGUI() {

GUI.skin = theSkin;

GUI.TextField(Rect(100, 200, 200, 20), theString, 3);
Word1();
}

static var lastChar : String = "";

function Word1() {
    if (Event.current.isKey) {
        if (lastChar != Event.current.character) {
            lastChar = Event.current.character.ToString();

            switch (lastChar) {
                case ("a"): theString += "a"; break;
                case ("b"): theString += "b"; break;
                case ("c"): theString += "c"; break;
                case ("d"): theString += "d"; break;
                case ("e"): theString += "e"; break;
                case ("f"): theString += "f"; break;
                case ("g"): theString += "g"; break;
                case ("h"): theString += "h"; break;
                case ("i"): theString += "i"; break;
                case ("j"): theString += "j"; break;
                case ("k"): theString += "k"; break;
                case ("l"): theString += "l"; break;
                case ("m"): theString += "m"; break;
                case ("n"): theString += "n"; break;
                case ("o"): theString += "o"; break;
                case ("p"): theString += "p"; break;
                case ("q"): theString += "q"; break;
                case ("r"): theString += "r"; break;
                case ("s"): theString += "s"; break;
                case ("t"): theString += "t"; break;
                case ("u"): theString += "u"; break;
                case ("v"): theString += "v"; break;
                case ("w"): theString += "w"; break;
                case ("x"): theString += "x"; break;
                case ("y"): theString += "y"; break;
                case ("z"): theString += "z"; break;
                case ("A"): theString += "A"; break;
                case ("B"): theString += "B"; break;
                case ("C"): theString += "C"; break;
                case ("D"): theString += "D"; break;
                case ("E"): theString += "E"; break;
                case ("F"): theString += "F"; break;
                case ("G"): theString += "G"; break;
                case ("H"): theString += "H"; break;
                case ("I"): theString += "I"; break;
                case ("J"): theString += "J"; break;
                case ("K"): theString += "K"; break;
                case ("L"): theString += "L"; break;
                case ("M"): theString += "M"; break;
                case ("N"): theString += "N"; break;
                case ("O"): theString += "O"; break;
                case ("P"): theString += "P"; break;
                case ("Q"): theString += "Q"; break;
                case ("R"): theString += "R"; break;
                case ("S"): theString += "S"; break;
                case ("T"): theString += "T"; break;
                case ("U"): theString += "U"; break;
                case ("V"): theString += "V"; break;
                case ("W"): theString += "W"; break;
                case ("X"): theString += "X"; break;
                case ("Y"): theString += "Y"; break;
                case ("Z"): theString += "Z"; break;

                default: break;
            }

            var compString : String = "The";
            var compString2 : String = "a";

            if (theString == compString) {
                The.GetComponent("TheGreen").enabled = true;
                }

            if (theString == compString2) {
                The.GetComponent("TheRed").enabled = true;
            }

            if (Input.GetKeyDown("backspace")){
                           // this is where I would like to delete one character at a time but dont know what script to use      

             The.GetComponent("TheClear").enabled = true;
            }

        }
    }
}

3 Answers

3

It looks like the formatting in your post got messed up. Try editing your post and reposting the code (making sure that all the code is correctly formatted).

You said using backspace to remove characters isn't working, but it doesn't look like you've actually implemented any code for that. Can you clarify what the problem is?

Oh, and I'm sure you don't need that big switch statement ;) I'm not sure what you're trying to do exactly, but I'm sure there's a more direct way to accomplish whatever it is you're trying to accomplish.

Wait, are you asking how to delete a character, or are you just saying that your current method isn't working? (Because you said it 'won't let you' delete a character, my assumption was that you have some code in place for this already, but that it's not working as you expect.)

Make use of this

Blockquote
Here is very simple solution to your answer, you just need to make “Text” in canvas and attach this simple script. download the attached script, it is in text format you just need to paste it int a c# script, make a script with name “Typing.cs” and past the code from the text file. hope your problem will be solved with this.

[63300-typing.txt|63300]

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
// written by hameed ullah jan
public class Typing : MonoBehaviour {
private float letterpause = 0.1f;
public AudioClip sound; // typing sound
private string mytext;
// Use this for initialization
void Start () {

	mytext  = "CHASE YOUR FIRST PASSENGER PLANE AND FUEL IT BEFORE IT FALLS";
	StartCoroutine ( TypeText ());
}

// Update is called once per frame
void Update () {

}

IEnumerator TypeText (){
	Debug.Log ("called");
	foreach(char letter in mytext.ToCharArray()){ 
		this.GetComponent<Text>().text += letter;
		if(sound)
		{
			if(!GetComponent<AudioSource>().isPlaying){
				GetComponent<AudioSource>().PlayOneShot(sound);
			}
			yield return new WaitForSeconds(letterpause);
		}
	}
}

}

Blockquote