- Home /
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;
}
}
}
}
Answer by Jesse Anders · Apr 18, 2011 at 01:31 AM
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.
There, fixed the formatting. And your definitely right that I dont need that big switch statement. I just didnt exactly know how to go about this. I am trying to make an educational game for a competition that $$anonymous$$ches kids typing and what I am trying to do is have a sentence above a GUI TextField that you have to type and match the words and for every word you type correctly I wanted to turn the word green showing that you typed it correctly. That part works but however when you make a mistake typing it and you hit the backspace button it wont delete the characters already typed.
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.)
Answer by hameed-ullah-jan · Feb 05, 2016 at 04:43 PM
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. [link text][1] 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 ()); }Blockquote
// Update is called once per frame void Update () {
} IEnumerator TypeText (){ Debug.Log ("called"); foreach(char letter in mytext.ToCharArray()){ this.GetComponent().text += letter; if(sound) { if(!GetComponent().isPlaying){ GetComponent().PlayOneShot(sound); } yield return new WaitForSeconds(letterpause); } } } } > Blockquote [1]: /storage/temp/63300-typing.txt
Your answer
Follow this Question
Related Questions
Boo help: method not defined 4 Answers
enable / disable a script without dynamic typing 2 Answers
"Type" type 1 Answer
How to set animations to loop? 1 Answer
Easy? Convert System.Type to UnityEngine.Component 3 Answers