• 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 /
  • Help Room /
This question was closed Apr 06, 2017 at 06:49 PM by Enry117 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Enry117 · Apr 05, 2017 at 06:05 PM · loops

if loop running when it shouldn't

In my videogame the player has to copy a text using the keyboard and the game, after checking the spelling of all the characters of all the words, updates the text to be copied in real time. So I created a List containing each charater of the text to be copied and a List containing all characters the player wrote in the input field. Then I created a series of loops to compare the two Lists character by character. These loops only have to tell me if the palyer wrote the right character, the wrong character, or if a character is missing. Here's the code i'm using:

 public void ErrorCheck()
     {
         //The method FillList() is a simple for loop which takes a List<char>, a string and an int n and fills the list with all the characters of the string for a number n of characters.
         listOfInputChar = FillList(listOfInputChar, inputText.text, inputText.text.Length);
         //Comparing listOfInputChar with listOfTextChar which was filled with the text to be copied in the Start() method.
         for (int i = 0; i < inputText.text.Length; i++)
         {
             //If the i-th char is correct.
             if (listOfInputChar[i] == listOfTextChar[i])
             {
                 print("correct");
                 continue;
             }
             //If the i-th char is not correct.
             else if (listOfInputChar[i] != listOfTextChar[i])
             {
                 //Missing char.
                 if (listOfInputChar[i] == listOfTextChar[i + 1])
                 {
                     print("missing character");
                 }
                 //Wrong character.
                 else
                 {
                     print("wrong character");
                 }
             }
         }
         //This script doesn't take in account corrections in the input field yet.
     }

I call this method in Update() and the problem is this code always run the "wrong character" loop for all the characters in the listOfInputChar with index from 2 and above, even tho the characters are all right. Whould you be so kind to help me with that? Why does that happen? Thank you in advance.

Comment
Add comment
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

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Enry117 · Apr 06, 2017 at 06:47 PM

Nevermind, I found the error: line 4 would be called every frame and in every frame the FillList() methont would just keep adding the same written characters over and over each frame instead of replacing them. So if the player would input only one letter, "A" for example, the listOfInputChar would fill up with "A"'s, one per each frame.

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

98 People are following this question.

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

Related Questions

From a scale from 1 to 10 how bad are loops inside loops 1 Answer

Instantiating game objects in spiral pattern 0 Answers

Coroutine with multiple Loops 2 Answers

trying to instantiate an object a random number of times ,how do i spawn something in a random ammount of times? 0 Answers

My Coroutine Ignores the Boolean 1 Answer


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