How can I pass data between two levels?

How do I pass information from my existing level to a new level that I'm about to load?

The way I approach this is by taking any properties that need to be accessed from more than one scene and making them public variables in a script that is added to an empty GameObject and then I call Object.DontDestroyOnLoad on that object so it will persist through scene changes. Then it's a simple matter of using GameObject.Find to get that `GameObject` from a script in the new scene and GameObject.GetComponent to get the script. Then you can access the public variables of a persistent object from any script.

P.S. - GameObject.Find is fast but there's no need to call it every frame. To reference the persistent object script from another script, do something like this:

SomeObjectInTheNewSceneScript.js (pick a better name)

// if your persistent script is named `PersistenScript.js`
private persistentScript : PersistentScript;

function Start () {
    // Start runs once early on and makes the connection to the other script
    // so you don't have to do it every frame.

    // if your persistent game object is named `Persistent Object`
    var persistentGameObject : GameObject = GameObject.Find("Persistent Object");
    persistentScript = persistentGameObject.GetComponent(PersistentScript);
}

then you can set/get properties of `persistentScript` anywhere else in `SomeObjectInTheNewSceneScript`

You can make a static class and work like PlayerPrefs to share objects between scene.

public class SharedObject {
		private static List<StringItem> stringCollections = new List<StringItem>();
		
		public static void SetString(string key, string value){
			foreach(StringItem stringItem in stringCollections){
				if(stringItem.key == key){
					stringCollections.Remove(stringItem);
					break;
				}
			}

			stringCollections.Add(new StringItem(key, value));
		}	
		
		public static string GetString(string key){
			string value = null;
			
			foreach(StringItem stringItem in stringCollections){
				if(stringItem.key == key){
					value = stringItem.value;
					break;
				}
			}
			
			return value;
		}
		
		private class ObjectItem{
			private string _key;
			
			public ObjectItem(string key){
				this._key = key;
			}
			
			public string key{
				get{ return _key; }
			}
		}
		
		private class StringItem : ObjectItem{
			private string _value;
			
			public StringItem(string key, string value) : base(key){
				this._value = value;
			}
			
			public string value{
				get{ return _value; }
			}
		} 
	}

Usage :
Send string to other scene SharedObject.SetString("Key", "String To Send");
Load string in scene BazzleConfig.SharedObject.GetString("Key")

You can create an object in first scene, attach a sript to it and sen value of variable in this script. For example if you want to send user login script attachet to game object that will survife after second scene load should be:

// User login:

public var userLogin : String;

function Awake() {

// Do not destroy this game object:

DontDestroyOnLoad(this);

}
Set userLogin in first scene and read in second.

Complete example can be found in my blog http://unity-tutorials.blogspot.com/. Listing 20 and 21.

Here’s the video Tutorial of what described by Rennat, which is implemented using c#

I’m not sure if this was mentioned or not but if it was I must of gotten confused and didn’t understand it sorry. When I use DontDestroyOnLoad() and then go to a new scene then go back to the original scene. There are two gameobjects that are the same. How do I just keep one of them and use them same one rather than creating the same gameobject every time I open up the scene?

@saifuddinazhar is right although the example he has given is quite complecated for Beginners…
@ryanroundhouse is also correct and it will become the best if you combine @saifuddinazhar and @ryanroundhouse

That is declaring something like this.

public static IDictionary myDictionary = new Dictionary();

for Beginners this might be way too complicated. so I suggest simple as this…

using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour {
	public static int level;
	public static float life;


	void OnLoadLevel (){
		level = 5;
		life = 20.5;
		Application.LoadLevel (1);
	}
}

On your next scene you should have the script to get this data using like this

using UnityEngine;
using System.Collections;

public class LoadStats : MonoBehaviour {
	int level;
	float life;


	void Awake (){
		level = GameManager.level;
		life = GameManager.life;
	}
}

You can also serialize an object with your data and save it as a file. Here is a link to a tutorial:
https://msdn.microsoft.com/en-us/library/4abbf6k0(v=vs.110).aspx

After that, you can deserialize the file to access persisted data when you load another scene.

Unity’s official tutorials now include an Adventure Game Tutorial. One of the final ‘phases’ in that tutorial discusses the creation of a “Persistent” scene with static Game State data.

Here a full tutorial on how to handle data between scenes: https://gamedevelopertips.com/how-to-handle-data-between-scenes-in-unity/

You will find a few examples and best practices.