- Home /
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")
{
cam.target = Car1;
Destroy(Car2);
Destroy(Car3);
}
else if (car == "EvoX")
{
cam.target = Car2;
Destroy(Car1);
Destroy(Car3);
}
else if (car == "Catamount")
{
cam.target = Car3;
Destroy(Car1);
Destroy(Car2);
}
}
function Start()
{
DontDestroyOnLoad(GameObject.Find("/RaceInfo"));
Info = GameObject.Find("/RaceInfo");
laps = Info.GetComponent("Menu").laps;
enemies = Info.GetComponent("Menu").enemies;
car = Info.GetComponent("car").ToString();
pickCar();
}
Am I doing something wrong here?
Add a #pragma strict to the top of the file and tell me what errors you get...
Answer by whydoidoit · Jun 26, 2012 at 05:30 PM
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.
Info.GetComponent(Menu).enemies
etc.
After adding #pragma strict & using the non string version of GetComponent "Info.GetComponent($$anonymous$$enu).enemies" I am getting NullReference Exceptions for all of those variables
So this isn't attached to a component that has those script on it I guess - or you can't find the RaceInfo...
Debug.Log both of those things..
I dont understand what you mean. what do I need to Debug.Log? what information are you looking for?
Thanks again btw for taking the time to help me :) it seems like you are the only one willing to help lol ;)
Hello A_C,
It looks like you are saying that the code above is placed in an empty game object in your second scene. Are you wanting to pass variables from scene 1 (your main menu scene) to scene 2? If so, you need to create an empty game object in scene 1 with the code DontDestroyOnLoad(this); in it. That should preserve a game object from one scene to the next. If you have an object named RaceInfo in your first scene that holds all of the information, make sure the DontDestroyOnLoad(this); is in it.
The RaceInfo game object is in the first scene, with DontDestroyOnLoad in its code. Im still having trouble accessing the info I want to preserve
Answer by Bunny83 · Jun 27, 2012 at 02:45 AM
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:
DontDestroyOnLoad(gameObject);
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.
"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."
Could you please explain how to do this? $$anonymous$$any thanks