简体   繁体   中英

How do I Pass a string variable Through Different Scenes? Unity C#

I Have 2 scripts. In one, you have a function where you can sign in. It needs a username and I want that username on a TMP Text that is located on the main menu scene.

    public string Username;

It IS a public string. Other script:

    public TextMeshProUGUI playerName;

And that is the TMP Text on which the player's username should be displayed.

I know how I could pass variables between scripts, but I don't know how to pass them through whole scenes.

Kinda rushed sorry.

You can try using PlayerPrefs .

Set players name on PlayerPrefs after login:

PlayerPrefs.SetString("Name", m_PlayerName);

Later load it on another scene from PlayerPrefs:

m_PlayerName = PlayerPrefs.GetString("Name", "No Name");

And if you would like to keep it persist between scenes PlayerPrefs would work as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM