- Home /
Why simple letter evaluataion returns true no matter witch letter
Hi,
I have a coroutine that runs through letters in a string and evaluates each to check if the letter is a dot, question mark or exclamation point and if the last letter was not any of those. The problem is that the evaluation returns true no matter what character it is evaluating.
char[] validStopLetters = { '.', '!', '?' };
if (letter == validStopLetters[0] | letter == validStopLetters[1] | letter == validStopLetters[2] & lastLetter != validStopLetters[0] | lastLetter != validStopLetters[1] | lastLetter != validStopLetters[2])
{
audioManager.Stop("Typing");
typingStopped = true;
yield return new WaitForSeconds(1);
}
else
{
yield return new WaitForSeconds(typingSpeed);
}
Thanks in advance for any explanation :)
Answer by bpaynom · Feb 24, 2019 at 01:40 PM
Try this
char[] validStopLetters = { '.', '!', '?' };
if ((letter == validStopLetters[0] || letter == validStopLetters[1] || letter == validStopLetters[2]) && (lastLetter != validStopLetters[0] && lastLetter != validStopLetters[1] && lastLetter != validStopLetters[2]))
{
audioManager.Stop("Typing");
typingStopped = true;
yield return new WaitForSeconds(1);
}
else
{
yield return new WaitForSeconds(typingSpeed);
}
Notice the ( and ) around first 3 comparisions, separating it from the last 3 (also have parenthesis around them). Notice to the double || and double &&.
What you where asking in your if was: if some of this is true, then :
letter == validStopLetters[0]
letter == validStopLetters[1]
letter == validStopLetters[2] & lastLetter != validStopLetters[0]
lastLetter != validStopLetters[1]
lastLetter != validStopLetters[2] Obviously, at least one of the last 2 would be true, so you always get TRUE.
Oh, sorry! I didn't notice the extra parentheses, this worked! Thanks!
Also, I noticed that the last 3 comparisions are irrelevant. You have to put && ins$$anonymous$$d of ||. I modify the answer.
Your answer
![](https://koobas.hobune.stream/wayback/20220612192350im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to resize a player every couple of seconds? 2 Answers
Problem comparing contents of arrays 1 Answer
How do i make two if statements 3 Answers