Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Kag359six · Apr 17, 2012 at 10:38 PM · stringif-statementsfor-loopsplit

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;
 
 }
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image IgnoranceIsBliss · Apr 17, 2012 at 10:55 PM 2
Share

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'?

avatar image Owen-Reynolds · Apr 18, 2012 at 12:16 PM 0
Share

Does only one "Guessed Right" appear, but never the second? It could be CollaspeIdenticalLines is still checked in your Debug window.

avatar image Kag359six · Apr 18, 2012 at 08:48 PM 0
Share

@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.

avatar image bodec · Apr 19, 2012 at 03:29 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges