- Home /
String comparision
I have a string "Hello World" which is displayed as white color in the game view. Now if I press e, e in Hello World should be highlighted i.e. e will become black.
Firs of all how to check which key i have pressed. Second I have to check if e is pressed then change e color in Hello World. I am using this script.
private string displayedStr, correctStr;
public GUIStyle stringStyle, stringStyle1;
void Start ()
{
displayedStr = "Hello World";
correctStr = "";
}
void Update ()
{
if(Input.anyKeyDown)
{
Debug.Log(Input.inputString);
for(int i=0; i<displayedStr.Length; i++)
{
if(Input.inputString.Contains(displayedStr[i].ToString()))
{
Debug.Log("Correct");
}
else
{
Debug.Log("Wrong");
}
}
}
}
void OnGUI()
{
GUI.Label(new Rect(300, 100, 100, 100), displayedStr, stringStyle);
GUI.Label(new Rect(300, 100, 100, 100), correctStr, stringStyle1);
}
I know I am doing lot of things wrong here. For correctStr I should check if that letter exists then only display that letter, something like that.
how to check if that character exists in the given string?
I don't think your question has anything to do with string comparison...
Answer by SubatomicHero · May 15, 2013 at 12:07 PM
try this:
if (displayedStr[i].ToString() == correctStr)
{
// etc etc
}
EDIT: You probably also want to make sure that correctStr wont get larger than 1 character at a time so in your update function you should check that if it is larger than 1 char, resize the string to 1 char.
Can you mark my answer as correct please so people searching the forums to help wont come back to this solved question :D
Answer by SubatomicHero · May 15, 2013 at 10:56 AM
// The first thing you do is check the character is in the string like you've done already, but in a different approach
correctStr = Input.inputString;
// assign the latest input into this string
for (int i = 0; i < displayedStr.Length; i++)
{
if (displayedStr[i] == correctStr)
{
Debug.Log("Correct.");
}
}
This could possibly help, but it needs testing.
it is showing this error... Operator ==' cannot be applied to operands of type
string' and `char'
yeah now its working fine... thanks.... now i have to work for change of text color
Answer by Graham-Dunnett · May 15, 2013 at 10:52 AM
Really??? The documentation is pretty good:
http://docs.unity3d.com/Documentation/ScriptReference/Input.GetKeyDown.html
The two examples there show how to check if the space key is down. If you need help figuring out when the e key is pressed, see:
http://docs.unity3d.com/Documentation/ScriptReference/KeyCode.html
Next, to change the colour, check out:
http://docs.unity3d.com/Documentation/Manual/StyledText.html
This tells you how you can change colours of text. The complication you have is that this does not work directly with GUI.Label
. You need to use a GUIStyle
, so see:
http://docs.unity3d.com/Documentation/Components/gui-Customization.html
to learn about that.
I want to check if that key exists in the given string or not. So i cant write $$anonymous$$eycode.E because there I cant write for all the 26 letters.
Functionality should be like this. I can press any key, for eg i am pressing 'a' which is not in the string it should give "wrong" message. Now in the next chance i press 'e' which is in the string, so it should give "correct" message.
Your answer
![](https://koobas.hobune.stream/wayback/20220613102243im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Compare two time strings? 2 Answers
How do I find a String in an Array? 2 Answers
Object reference not set... but... it is? 0 Answers
Problem with makeing text appear on screen 0 Answers
String to Var 2 Answers