It is possible to use a static class for data persistence throughout scenes. You can declare a class Userdata, for example, and use static properties to store this data, for example:
class UserData{
// Nome de usuário do jogador.
public static string userName = "";
// Password do jogador.
public static string password = "";
}
No need to inherit Monobehaviour in that case, nor attach the script to an object (incidentally not inheriting Monobehaviour nor will it be possible to attach it).
This way, in Cena1 you probably already have one script with a method that will be executed at the click of the button that makes the switch to Cena2. Before loading Cena2 you can store your information, for example:
public void TrocaDeCena(){
// Armazena os dados do usuário.
UserData.userName = campoDeTextoUsername.text; // Aqui você já deve ter a referência do campo de texto onde o usuário digitou.
UserData.password = campoDeTextoPassword.text; // Aqui você já deve ter a referência do campo de texto onde o usuário digitou.
// Carrega a Cena2.
SceneManager.LoadScene("Cena2");
}
To recover the data saved in the static class, in the next scene in the method Start of some script attached to the UI element that will display the information you will have a code similar to the following:
public void Start(){
// Aqui você já deve ter a referência do elemento text que exibirá o dado.
// Exibe o nome do usuário armazenado.
labelUserName.text = UserData.userName;
}
This is one of the simplest ways to traffic/persist data between scenes. How to get text field values and how to assign values in Labels can change according to your version of Unity but the static class approach works since very old versions.
In Cena1, I put a variable like playernome that is assigned with text field that has been filled in, for example: playernome = txtnome.tex.Trim(); Playerprefs.Setstring("Name", playernome);. To put on the label would be like this: lblnome.text = Playerprefs.Getstring("Name")? I will do a test, and will give a test return.
– ARTHUR HENRIQUE VIEIRA CAMBRAI
That’s right!
– anderson seibert