Your First Solo: Stack overflow on my code

Hey guys!

So I’m attempting to run this code but I seem to be getting a stack overflow. Not sure what that means or where the issue is.

Thanks! :slight_smile:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Hacker : MonoBehaviour
{
    // Game State
    int level;
    enum Screen { MainMenu, Password, Win };
    Screen currentScreen;

    // Start is called before the first frame update
    void Start()
    {
        ShowMainMenu ();
    }

    void ShowMainMenu()
    {
        currentScreen = Screen.MainMenu;
        Terminal.ClearScreen();
        Terminal.WriteLine("Sup yo");
        Terminal.WriteLine("You need to hack into these servers");
        Terminal.WriteLine("Press 1 to access the School");
        Terminal.WriteLine("Press 2 to access the Museum");
        Terminal.WriteLine("Press 3 to acces the Government");
    }


    void OnUserInput(string input)
    {
        if (input == "menu") // we can always go direct to main menu
        {
            level = 0;
            ShowMainMenu();
        }
        else if (currentScreen == Screen.MainMenu) // This runs the main menu
        {
            RunMainMenu(input);
        }

    }

    void RunMainMenu(string input) // Main menu is controlled here
    {
        if (input == "1")
        {
            level = 1;
            StartGame(input);
        }
        else if (input == "2")
        {
            level = 2;
            StartGame(input);
        }
        else if (input == "3")
        {
            level = 3;
            StartGame(input);
        }
        else if (input == "117")
        {
            Terminal.WriteLine("Wake me when you need me.");
        }
        else
        {
            Terminal.WriteLine("Please choose a valid option.");
        }
    }

    void StartGame(string input)
    {
        currentScreen = Screen.Password;
        Terminal.WriteLine("You have chosen level " + level);
        Terminal.WriteLine("Please enter password: ");
        if (level == 1)
        {
            RunEasy(input);
        }
        if (level == 2)
        {
            RunMedium(input);
        }
        if (level == 3)
        {
            RunHard(input);
        }
    }

    void RunEasy(string input)
    {
        Terminal.WriteLine("alscs");
        if (input == "class")
        {
            Terminal.WriteLine("Password Successful.");
            ShowMainMenu();
        }
        else
        {
            Terminal.WriteLine("Fail. Please Try Again:");
            RunEasy(input);
        }
    }
    void RunMedium(string input)
    {
        Terminal.WriteLine("itgnpain");
        if (input == "painting")
        {
            Terminal.WriteLine("Password Successful");
            ShowMainMenu();
        }
        else
        {
            Terminal.WriteLine("Fail. Please Try Again:");
            RunMedium(input);
        }
    }
    void RunHard(string input)
    {
        Terminal.WriteLine("isetalnrpsed");
        if (input == "presidential")
        {
            Terminal.WriteLine("Password Successful");
            ShowMainMenu();
        }
        else
        {
            Terminal.WriteLine("Fail. Please Try Again:");
            RunHard(input);
        }
    }


    // Update is called once per frame
    void Update()
    {

    }
}

This is what Stack Overflow means.

I don’t want to tell you the answer as to where is your error, you’ll learn a lot more if you figure it out by yourself, but I’ll give a hint: You have three methods that run infinitely, (they never stop running which causes the stack overflow), check your logic on RunEasy, RunMedium and RunHard, you are calling something in those 3 methods that shouldn’t be called.

1 Like

Found it! Thank you. If I am understanding clearly, you cannot run a method within a method.
In my mind, I thought it would reset the method from scratch.

I seem to be running into another issue now though. When I run the game, once I write an input in the terminal, “1, 2, or 3”, the code doesn’t seem to wait for me and automatically jumps to “Fail. Please Try Again.”

I’m not totally sure why it won’t wait for my input. I feel like it’s an obvious answer, I just can’t seem to figure it out. I tried using “else if”, but I am getting a red underline when I do that.

Thanks for your help!

This challenge is actually quite hard, just keep at it, you’ll figure it out! :smiley:
And if you don’t, don’t worry, is part of the learning process! Also, keep asking!

1 Like

Thank you! :slight_smile: Appreciate your help

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms