Load Scene With Parameters | Unity Load Scene With Data
Hello everyone, Want to learn How to transfer data between scenes. Don’t worry, we are here to teach you how you can transfer data between two scenes. Let’s understand your problem first.
You are making a game with multiple scenes. And now you want to load a scene but with some values with it. So that you can access that data in other scene and use it. We will teach you Load Scene With Parameters Ok, so we are going to tell two ways for this.
Transfer data between scene using PlayerPrefs ( only string, int and float)
This method is a easy method lf transferring data between multiple scenes. Lets see you have Scene_1 and Scene_2. Currently you are in Scene_1 and Now we will load Scene_2 With a data “playerHealth”. But first lets create a gameObjects in both scenes and make two scripts for both scenes. Lets say Scene1Script.cs for Scene_1 and Scene2Script.cs for Scene_2.
Scene1Script.cs
int playerHealth;
private void LoadSceneWithData(){
PlayerPrefs.setString("playerHealth",100);
SceneManager.LoadScene("Scene_2");}
Now open Scene2Script.cs
Scene2Script.cs
int playerHealth;
private void Start(){
playerHealth = PlayerPrefs.GetString("playerHealth");
}
So in above example, you sent a data called playerHealth from Scene_1 to Scene_2 using PlayerPrefs.
Load Scene With data using static variable
Using this method you can transfer string, float, int, Vector3 and Vector2. You have to create a Script with some static variables. Lets see we create a Script called MyValues.cs. Now open MyValues.cs and write something like this.
MyValues.cs
public static int playerHealth;public static Vector3 playerPosition;
public static int enemyCount;
Now you can set data here before loading new scene. Now let’s assume you are in Scene_1. Open Scene1Script.cs where you are using a method to start new Scene( Scene_2)
private void LoadSceneWithData(){
SceneManager.LoadScene("Scene_2");
MyValues.playerHealth = 100;
MyValues.enemyCount = 10;
MyValues.playePosition = Vector3.zero;
}
And now for accessing these values, in start method use this
int playerHealth;
int enemyCount;
Vector3 playerPosition;private void Start(){
playerHealth = MyValues.playerHealth ;
enemyCount = MyValues.enemyCount;
playerPosition = MyValues.playePosition;
}
Use of above function
Loading New Scene with data can be useful in alot and ways. For example, transferring a current health of player to new scene so that player spawn in new scene with current health. For example you are making a dungeon game and after finishing last room, you want to load new scene but want to keep player health same. So you can use above method.
Also if you want to decide number of enemies to spawn in new scene, then this method is very helpful for you. Set the value of enemies in first scene ( before calling LoadScene ) and in new scene, call somewhere in start() method.
You can decide the position for player in new Scene so that player spawn at specific position.
You can tell new scene that is there day or night. You can transfer a Boolean by telling is it day or not. And in new scene, set SkyBox, ligh intensity and color.
These are two easy methods for transferring data between scenes in unity. There can be more ways for this task so you can figure out your new way. Remember one thing, in programming there are a lot of ways to get same result. So learn more and make your way to become successful programmer. Thanks for visiting us.