- Home /
if statement not returning true
Im trying to make a hangman like game. When User enters a letter, if it corresponds with the correct letter in the word they are trying to guess, a debug log says "guessed right". This is a chunk of the code thats suppose to do that: However even though the letters match up in game, the debug log doesnt show up.
function CheckIfCorrect() {
splitWord = SplitString(word);
for(i = 0; i < splitWord.length; i++)
{
Debug.Log(underScore[i]);
Debug.Log(splitWord[i]);
if(underScore[i] == splitWord[i])
{
Debug.Log("Guessed Right");
}
}
listOfLetters.Clear();
}
function SplitString(sentence) {
for(letter in sentence)
{
listOfLetters.Push(letter);
}
Debug.Log(listOfLetters);
return listOfLetters;
}
Strings can already be accesed as arrays - you shouldn't need to use your 'SplitString' function.
Are you getting the debug entries for 'underScore' and 'splitWord'?
Does only one "Guessed Right" appear, but never the second? It could be CollaspeIdenticalLines is still checked in your Debug window.
@IgnoranceIsBliss I am getting a debug entry for both underscore and splitword but i will try what you said and not use my split function. @Owen Reynolds the "guessed right" log doesnt appear at all. Further dissection of the issue shows that it can't find underscore[] in splitword[] at all.
looks like when you run your loop it gts stopped at the if statement try guessing the first letter if it works and debugs then its your if statement asking if [i]is equal. a expample would be on the word "dog" if [i] == o then your if statement is going to return false and the code stops.
Answer by Kleptomaniac · Apr 19, 2012 at 07:40 AM
I would suggest cleaning this up. As @IgnoranceIsBliss said, strings can already be accessed as arrays. Therefore you can eliminate your SplitString function. Also, I'm assuming that underScore is the inputted letter? If so, your if statement would only return true if both indexes lined up in each array and were equal to each other. For example, if underScore = "a"
, and splitWord = "banana"
, checking for whether underScore[i] == splitWord[i]
would not work because the index of "a" in underScore is 0 while the only a's in splitWord are 1, 3, and 5. Therefore, you need to check if the element in splitWord is equal to underScore as a whole string. So like this:
function CheckIfCorrect() {
for(i = 0; i < splitWord.Length; i++)
{
if(splitWord[i] == underScore)
{
Debug.Log("Guessed Right");
}
}
listOfLetters.Clear();
}
This returns "Guessed Right" for me. :) Also, just in case that doesn't work, ensure you're actually calling your function. However I doubt that will be a problem. :D
Hope that helped, Klep
Your answer
Follow this Question
Related Questions
Splitting String Into Array 2 Answers
For loop on text without effecting each other 1 Answer
disperse string by letter 1 Answer
How to split alphanumeric string into array of strings based from their type : alpha or numeric ? 1 Answer
Comparing two Strings. Returns equal and not equal at the same time? 3 Answers