Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question How do I write a script to check if input box entry is correct?

Discussion in 'Scripting' started by sararaz, Apr 2, 2024.

  1. sararaz

    sararaz

    Joined:
    Mar 13, 2024
    Posts:
    5
    Hello everyone, I'm writing a script within my maths game to take in an entry from the user into an input box and compare it to a saved string (which is the answer to the question they're inputting an answer to) and then move the stage on when they answer correctly or change the question if answered incorrectly. But I can't get it to compare the inputted string to the stored one, and I'm not sure what the issue is.
    This is a link to a gallery with all the images of the scripts associated with the problem and the error I get when I try to load the scene: https://imgur.com/a/M2u7eo3
    When it loads the last question in the question manager (Element 2), the game also displays an out of index issue. If it loads any other question, it changes the question regardless of the input, which is what I want it to do when the question's answered incorrectly but even when the input's "correct", it still does that.
    I'm very new to unity and visual studio and c#, i apologise if something's unclear or isn't implemented well, thank you so much in advance :oops:
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,008
    What does this mean? You can't get the syntax correct? We know string equality tests did not break so what does "can't get it to" mean?

    If you are doing math questions you may wish to convert the string to a numeric value before testing it. Google for how to turn a string into an integer or even a floating point number.

    Fix that first. Here are some notes on IndexOutOfRangeException and ArgumentOutOfRangeException:

    http://plbm.com/?p=236

    Steps to success:
    - find which collection it is and what line of code accesses it <--- (critical first step!)
    - find out why it has fewer items than you expect
    - fix whatever logic is making the indexing value exceed the collection size
    - remember that a collection with ZERO elements cannot be indexed at all: it is empty
    - remember you might have more than one instance of this script in your scene/prefab
    - remember the collection may be used in more than one location in the code
    - remember that indices start at ZERO (0) and go to the count / length minus 1.

    This means with three (3) elements in your collection, they are numbered 0, 1, and 2 only.