Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Game Text Inputfield and Button not responding

Tags:

c#

I am trying to build a word guessing game. My input field is supposed to accept a string and go to the next page if right or return back to the menu page if not right. Currently, my scene is going to the next scene for both the right and wrong answer.

public class anser : MonoBehaviour {

    private string secretWord = "eje";
    private string guessWord;

    public void GetInput(string guessWord) {
        //Invoke ("CompareGuesses", 1f);

        if (secretWord == guessWord) {

            SceneManager.LoadScene ("Front Page");

        } else if (secretWord != guessWord) {

            SceneManager.LoadScene ("Question2");
        }
    }

}
like image 312
Bumsque Avatar asked Feb 25 '26 23:02

Bumsque


1 Answers

I'm not sure what the guessWord field is for, all you need is the guessWord parameter for your GetInput() method.

Also, you need to make sure the scene names are correct (check for capitals and spaces), and make sure that some other script is actually calling the GetInput() method (you didn't post the code for anything that calls this method).

Here is a cleaned up version of your code that should do what you want:

// The class name "anser" was misspelled. Also you typically use PascalCase for class names.
public class Answer : MonoBehaviour
{
    private string secretWord = "eje";

    public void GetInput(string guessWord) 
    {
        if (secretWord == guessWord.ToLower()) 
        {
            SceneManager.LoadScene("Question2");
            return; // eliminates the need for an else clause
        } 

        SceneManager.LoadScene("Front Page");
    }
}

Edit: Per Scriven's suggestions in the comments, I changed the LoadScene() arguments to reflect the OP's desired behavior. Also added a little bit of input validation for the guessWord parameter.

like image 63
Lews Therin Avatar answered Feb 27 '26 12:02

Lews Therin