0

So, I'm trying to load the next scene by pressing a button on screen. Earlier, I had it so when the user presses enter, the scene will load, but after realizing that wouldn't work too smoothly on iPads, I decided to switch to buttons. But the button method makes the next scene nearly two minutes to load, while the press Enter method was nearly instantaneous. Anyone have any idea why that is?

//Press Enter method

if (Input.GetKeyDown(KeyCode.Tab))
{
    //If there's a space in the word, display a message and don't progress
    if (input.text.Contains(" "))
        warningWord.text = "Words cannot contain spaces!";
    //If the word is valid...
    else if (!string.IsNullOrEmpty(input.text))
    {
        //Display no error message
        warningWord.text = ("");
        //Set the string to the inputfield's contents
        wordToSpell = input.text;
        //Show the list of already typed words
        typedWords.text = typedWords.text + "\n" + input.text;
        //clear the input field
        input.text = "";
        //Set the word to all caps in order to keep it simple
        wordToSpell = wordToSpell.ToUpper();
        //Add the word to the list of words
        wordList.Add(wordToSpell);
    } 
}
//If the Return (Enter) key is pressed...
if (Input.GetKeyDown(KeyCode.Return))
{
    //If the user hasn't entered any words, display a message and don't progress
    if (wordList.Count == 0)
        warningWord.text = ("You haven't entered any words!");
    //If the user has not selected a difficulty, display a message and don't progress
    if (!easy.isOn && !medium.isOn && !hard.isOn)
        warningDifficulty.text = ("You haven't selected a difficulty!");
    //If a difficulty is selected and there are words that have been entered...
    if ((easy.isOn || medium.isOn || hard.isOn) && wordList.Count > 0)
    {
        //Set the backup list to the real list
        backupList = new List<string>(wordList);
        //Set the difficulties depending on the selected bubbles
        if (easy.isOn)
            PlayerPrefs.SetString("difficulty", "easy");
        else if (medium.isOn)
            PlayerPrefs.SetString("difficulty", "medium");
        else if (hard.isOn)
            PlayerPrefs.SetString("difficulty", "hard");
        //Make the canvas invisible as to hide the text boxes in the later scenes
        canvas.gameObject.SetActive(false);
        //Load the game
        SceneManager.LoadScene(1);
    }

}


//Button method
void Update()
{
    //As long as a difficulty is selected, don't display an error message
    if (easy.isOn || medium.isOn || hard.isOn)
        warningDifficulty.text = "";
    //If the enter word button is pressed...
    enterWord.onClick.AddListener(EnterButton);
    //If the start game button is pressed...
    startGame.onClick.AddListener(StartButton);
}
void EnterButton()
{
    //If there's a space in the word, display a message and don't progress
    if (input.text.Contains(" "))
        warningWord.text = "Words cannot contain spaces!";
    //If the word is valid...
    else if (!string.IsNullOrEmpty(input.text))
    {
        //Display no error message
        warningWord.text = ("");
        //Set the string to the inputfield's contents
        wordToSpell = input.text;
        //Show the list of already typed words
        typedWords.text = typedWords.text + "\n" + input.text;
        //clear the input field
        input.text = "";
        //Set the word to all caps in order to keep it simple
        wordToSpell = wordToSpell.ToUpper();
        //Add the word to the list of words
        wordList.Add(wordToSpell);
    }
}
void StartButton()
{
    //If the user hasn't entered any words, display a message and don't progress
    if (wordList.Count == 0)
        warningWord.text = ("You haven't entered any words!");
    //If the user has not selected a difficulty, display a message and don't progress
    if (!easy.isOn && !medium.isOn && !hard.isOn)
        warningDifficulty.text = ("You haven't selected a difficulty!");
    //If a difficulty is selected and there are words that have been entered...
    if ((easy.isOn || medium.isOn || hard.isOn) && wordList.Count > 0)
    {
        //Set the backup list to the real list
        backupList = new List<string>(wordList);
        //Set the difficulties depending on the selected bubbles
        if (easy.isOn)
            PlayerPrefs.SetString("difficulty", "easy");
        else if (medium.isOn)
            PlayerPrefs.SetString("difficulty", "medium");
        else if (hard.isOn)
            PlayerPrefs.SetString("difficulty", "hard");
        //Make the canvas invisible as to hide the text boxes in the later scenes
        canvas.gameObject.SetActive(false);
        //Load the game
        SceneManager.LoadScene(1);
    }
}
KamikyIT
  • 314
  • 2
  • 15
Mike OD
  • 117
  • 1
  • 12
  • Glitch100 is right. You are adding this multiple times. The **#2** from the linked answer shows you how to detect which button is clicked with one function. You got that wrong with your current code. – Programmer Jul 11 '17 at 19:06

1 Answers1

2

I am going to go out on a limb and say that your Update loop is causing problems. You are calling the AddListener method on every frame. That should be done on the Start method so it's only done once, and removed when the GameObject is removed from the scene.

So it's quite possible it is just adding the same method multiple times and then when you click the button it is trying to load call that method x times

You can see the various events available on the MonoBehaviour class here in their documentation

JEV
  • 2,494
  • 4
  • 33
  • 47
  • Even if that's not the cause of THIS issue, that's still *really* bad. – krillgar Jul 11 '17 at 16:09
  • Yea, I want to believe it's the cause. It's a bit mad – JEV Jul 11 '17 at 16:10
  • Wait but isn't the listener only being added onClick? That line should just be skipped over if the button isn't clicked, right? – Mike OD Jul 11 '17 at 16:46
  • @MikeOD no, anything in an `Update` method is hit every single frame.If this answers your question, please accept as answer, thanks – JEV Jul 11 '17 at 20:29