Unity 3d: Passing information from one scene to another

Asked

Viewed 3,147 times

0

My dear(a), I did a search to find troubleshooting that I couldn’t find in another source found on google. The idea is when a user fills the information in a scene identified as cena1 that contains UI object, such as the case of access authentication screen and among all things. When you click the button, move to another scene as cena2 containing the displayed information that was populated in a cena1. For example: I typed in text field "John" and when I click button confirm that will pass to cena2. And in cena2 displays John on a label or label. has some generic functions that does this?

3 answers

2

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.

2

You can do this using the function:

PlayerPrefs.SetString("Nome", variavelNome);

To save in scene 1, and then in scene 2 use:

PlayerPrefs.GetString("Nome");

To manipulate the information!

  • 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.

  • That’s right!

1

Arthur,

to solve this problem you can use an empty object with a script GameController to save the information of the first scene. With the script created just add the method Dontdestroyonload so that when you load to cena2, you still have the object GameController active on the scene. Adding the Patterns Singleton design, you can refer to the class methods of any script, without having to have the reference on the scene.

Ex: GameController.Instance.GravarNome("teste");

public class GameController : MonoBehaviour
{
    private string nome = "";

    private static GameController instance = null;
    public static GameController Instance { get { return instance; } }

    void Start()
    {
        instance = this;
        DontDestroyOnLoad(this.gameObject);
    }

    public void GravarNome(string nome)
    {         
        this.nome = nome;
    }

    public string ConsultarNome(){
        return this.nome;
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.