- Home /
Connecting scenes
Alright, what we are attempting to create is a Space Sim. The player starts in Sol System (Earth) and can travel through 15 different maps.
We have a phase gate (at least one in each of the maps, maybe more) that will take you to different star systems.
What we are curious, is how do we create the new scene? (which I think I found the answer by pressing control-N) And then link the new scene with the phase gates? We want the player to travel to ANY system using these gates.
The goal is to eventually add mulitplayer capability, but thats far away! Any help would be greatly appreciated, and thanks in advance.
Answer by burnumd · Nov 23, 2009 at 04:30 PM
You've got the right idea so far. Create all your scenes in the Unity Editor (either with cmd (OSX) or ctrl (Win) + n or from File->New Scene).
Create the scene order by opening the build menu (cmd/ctrl + shift + B or File->BuildSettings...) and dragging scenes from the Project view into the build menu. You can change the order by dragging them up and down in the build menu.
You can now load new levels using the Application.LoadLevel() function. You can use either the scene name or its order in the build settings to determine which level to load. A simple example follows:
var levelToLoad;
function OnTriggerEnter (other : Collider) { //Check to see if a player entered the gate, rather than some space debris. if (other.gameObject.CompareTag("Player")) { Application.LoadLevel(levelToLoad); } }
Where "levelToLoad" is either a scene name (enclosed in quotes) or a number. The variable can be set in the script or in the inspector when the script is added to some GameObject (in your case, a gate).
One caveat: be sure you've read the collision detection matrix here to ensure you've added the correct components, or else OnTriggerEnter won't fire at all. If you have questions about that aspect of the editor or have trouble understanding the documentation, it would be best to search for or start a new question on that topic. Here might be a useful place to start.
I tried to apply the above code, changed what I needed to change, but when I enter the phase gate, it tells me:
"Level newscene couldn't be loaded because it is not added to the build settings. To add a level to the build settings use the menu File->Build Settings..."
I have the scene added to the build settings, but still will not load. I also compiled it, and ran it, and it wouldn't do anything. Am I doing something wrong?
Using LoadLevel() with a string requires a case sensitive argument, so if the string doesn't exactly match, you'll run into problems. One thing you might test is to temporarily replacing the string argument with the desired scene's numerical order in the build settings. You'll probably also want to specify that you're using an int (change "var levelToLoad;" to "var levelToLoad : int"). If it doesn't load the level using that approach, let me know.
I ended up getting it too work. I forgot to add the " " around the player argument.
Now is there a way, say to allow a choice menu to appear on which star system they wanted to go and have it linked to say 3 different scenes?
I created a new thread for this question at http://answers.unity3d.com/questions/752/how-can-i-allow-a-player-to-select-from-multiple-scenes-to-load since it's a different question (primarily regarding the GUI system) and should get tagged properly
Please note that you can also use the Scene file name ins$$anonymous$$d of a scene number. Just be careful not to rename files without rena$$anonymous$$g the reference...
Answer by Ehren · Nov 23, 2009 at 04:29 PM
You can use Application.LoadLevel to load your other scenes on demand.
Your answer
Follow this Question
Related Questions
Scene Connecting 5 Answers
Linking a prefab to another prefab in deep hierarchy using Inspector 1 Answer
GetPersistentManager problem? Can't save a scene anymore 1 Answer
Callback for when an object is deleted from the scene by the user in EditorMode. 3 Answers
Unity takes forever to load scene 1 Answer