Passing information between scenes?

I am making a racing game. The first scene is the main menu. The user chooses the track, their car, number of opponents and the number of laps, and then the track they would race on is loaded. I am saving that information attached to an empty game object that is set to DontDestroyOnLoad in my second scene, but it seems that the information is not being preserved, or I am not accessing it right.

This is the script attached to my terrain in the Second scene I am trying to load:

var AICar1 : GameObject;
var AICar2 : GameObject;
var AICar3 : GameObject;
var Car1 : GameObject;
var Car2 : GameObject;
var Car3 : GameObject;

var Info : GameObject;

var cam : GameObject;
var car : String;
var laps : int;
var enemies : int;

function pickCar()
	if (car == "Dodge Charger")
	{ = Car1;
	else if (car == "EvoX")
	{ = Car2;
	else if (car == "Catamount")
	{ = Car3;
function Start()
	Info = GameObject.Find("/RaceInfo");
	laps = Info.GetComponent("Menu").laps;
	enemies = Info.GetComponent("Menu").enemies;
	car = Info.GetComponent("car").ToString();

Am I doing something wrong here?

Add a #pragma strict and I’m sure you will find that laps, enemies etc aren’t found. You need to use the none string version of GetComponent.



You want to use DontDestroyOnLoad in the second scene on an object from the first scene? That doesn’t make much sense. LoadLevel will destroy all objects before the new one is loaded. If you want to preserve something from a scene, it has to be marked as DontDestroyOnLoad in the old scene. Usually you would do something like:


Next thing is, if you plan to load the scene multiple times, you get in trouble since the object is preserved from the last time the level was loaded and it’s created again when the level is loaded again. So you have two objects (or three or more, depening how often you load the level).

You might want to place such a script in a dedicated start scene. This scene is loaded only once when the game is loaded. This scene contains your data script and just loads your mainmenu scene. This way you can switch between your levels and the mainmenu without problems.