Working with Scenes

I’ve figured out how to use Application.LoadLevel to switch scenes. However, this command appears to unload the old scene. If I want to display a menu within my game play (like and option menu), what is the best approach?

Am I suppose to use Object.DontDestroyOnLoad ? New to Unity…

Yes, that is one way to do it. You can also use LoadLevelAdditive to load another level on top of the current one.

DontDestroyOnLoad is probably your best bet if you want to reuse the same menu in every scene.

If I use LoadLevelAdditive to load a scene onto another scene, is there a way to unload the second scene when I’m done with it (only the first scene should exist)?

For something like a menu that should be instantly available, I’d go with just having it be part of the first scene and then setting DontDestroyOnLoad so that it stays with subsequent scenes.

Coming from XNA, I’m not sure how you go about mixing game play and menus into a single Unity scene (assume that’s what you are saying). Can you explain? It would seem more logic to have these thing in separate scenes (and switch between the scenes)?

XNA isn’t Unity. Things work differently.

Doing what he suggests make sense, and makes life easier. Check the documentation regarding loading levels, specifically additive loading.

Have the menu disabled normally. When they press Esc or whatever, pause the gameplay somehow and display enable the options menu game object. How you pause the gameplay is pretty heavily dependent on how you have your game setup. If everything is based on Time.deltaTime then maybe just setting the time scale down to 0 would work. Or if you have some master script that coordinates the actions of all the other scripts, then setting a pause variable in there could work.

Be aware that there might be some potential problems with setting the time scale down to 0. I don’t remember exactly what, but I know I remember people saying some stuff stopped working when the time scale was 0.

The way a lot of people do it in Unity, like the Bootcamp demo for example is use a single gameObject called “GameManager” or something similar, and this one gameObject, with children as well holds all the data though out all the levels.

Now you obviously attach “DontDestroyOnLoad” to this gameObject, but for advanced level control such as the menu changes depends on the level isn’t to complicated either. using such classes as “Application.loadedLevelName”, you could make a simple switch case.

C#: Should work haven’t tested

switch(Application.loadedLevelName)
{
	case "levelOne":
		if(GUI.Button(new Rect((Screen.width)/2, (Screen.height)/2, 200, 20), "Change to Level Two"))	
			Application.LoadLevel("levelTwo");
		break;
	
	case "levelTwo":
		if(GUI.Button(new Rect((Screen.width)/2, (Screen.height)/2, 200, 20), "Change back to Level One"))	
			Application.LoadLevel("levelOne");
		break;
	default:
		break;
}

@niosop and @Tomme, Thanks. Very helpful! Also, I found the Bootcamp sample.