How to Salve INPUT so i can use it on other Function?

This is my code, i’m trying to use the “Input” in other function but i’m not being able to

public class Hacker : MonoBehaviour
{
string Ordem ;
string Nome = “Bem vindo Oz”;
// Start is called before the first frame update
void Start()
{
MostrarMenuInicial(Nome);

}

void MostrarMenuInicial(string cumprimento)
{
    Terminal.ClearScreen();
    Terminal.WriteLine(cumprimento);
    Terminal.WriteLine("Qual servidor voce gostaria de hackear?");
    Terminal.WriteLine("Aperte 1 - Servidor finanças");
    Terminal.WriteLine("Aperte 2 - Servidor do Banco");
    Terminal.WriteLine("Aperte 3 - Servidor Internacional");
    Terminal.WriteLine("Escolha sua opçao:");
}



void OnUserInput(string Input)
{
    if (Input == "menu")
    {
        MostrarMenuInicial(Nome);
    }
    else if (Input == "1")
    {
        StartLevel(1);
    }
    else if (Input == "2")
    {
        Terminal.WriteLine("Voce escolheu o Servidor do Banco");
    }
    else if (Input == "3")
    {
        Terminal.WriteLine("Voce escolheu o Servidor Internacional");
    }
    else 
    {
        Terminal.WriteLine("Voce escolheu uma opçao invalida");
    }
}

private void StartLevel(int level)
{
    if (level == 1)
    {
        Terminal.WriteLine("Voce escolheu o Servidor finanças");
        Terminal.WriteLine("0 para confirmar 1 para retornar ao menu");
        if (Input == "0")           <----------- Here Is The Problem
        {
            MostrarMenuInicial(Nome);
        }
        else if (Input == "1")          <----------- Here Is The Problem
        {
            IniciarLevel1();
        }
    }
}

private void IniciarLevel1()
{
    throw new NotImplementedException();
}

}

Hi Oziel,

Input is a local variable existing in the scope of your OnUserInput method only. If you want to pass on the user input, add a second parameter to StartLevel:

private void StartLevel(int level, string input)
{
    // your code
}

When you call the method, pass on the Input string as the second argument: StartLevel(1, Input);.


See also:

1 Like

Thx!

Is ther a way to save to input outside of my OnUserInput method? i did that to my int level, i created a int level outside so it culd be saved and caled upon, but i cant use the same thing for the input , so i’m doing just like you showed, but i want to know if there is a diferent way to do it.

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