I want to pass a data (string) to another scene in Unity. The problem is that these two scenes don't intersect with each other, meaning if you choose the first scene, even though I put DontDestroyOnLoad() to the script I want to fetch from, it will be useless because I won't be on the second scene where I want to fetch the data to anymore. It's either go to THIS scene or go to THAT scene. They don't know each other. Is there any workaround for this?
Asked
Active
Viewed 402 times
1 Answers
0
In your first scene, you can put a PlayerPrefs and it's very easy
PlayerPrefs.SetInt("StringName", stringToPass);
With this line, you are saving your string (stringToPass) permanently even if you close the game as the name of "StringName"
In the new scene you can then call it as
PlayerPrefs.GetInt("StringName", "noString");
With this you are calling the string you saved in the "StringName" and, if you haven't ever put a string inside it it will return "noString";
You can save it to a new string also
newString = PlayerPrefs.GetString("StringName", "emptyString");

Leoverload
- 1,194
- 3
- 8
- 21
-
I can't use PlayerPrefs because in my case, my game should not have a default value because it depends on whenver someone creates a room (Photon). It does not hold data when the project has been built. It just does it locally – Virtual Dreamer Dec 17 '20 at 08:46
-
https://answers.unity.com/questions/1221717/playerprefs-not-working-on-android-2.html check this out – Virtual Dreamer Dec 17 '20 at 08:51
-
@VirtualDreamer then you can set the defaul value as the room creation value. And reset it every time so it is not storing. You can also use static strings for this – Leoverload Dec 17 '20 at 08:52