- Home /
Application.LoadLevel loading the current scene instead of the specifed one
I am working on a game where players take turns firing cannonballs at each others castle. After Player 1 fires, I want the game to wait a few seconds, then load player 2's scene, and vice versa.
I used Application.LoadLevel to try to do this, but instead of loading the level I try to pass in, it reloads the current scene. My scenes are called Player1 and Player2, and both are loaded into the build settings. I have tried calling Application.LoadLevel with both the build ID for the scenes and their names, but neither seems to actually cause the correct level to load.
Here is my code:
var activePlayer : String = EditorApplication.currentScene; Debug.Log(activePlayer); if (activePlayer == "Assets/Scenes/Player1.unity"){ yield WaitForSeconds (6); Application.LoadLevel("Player2"); print("Player 2's Turn"); } else { yield WaitForSeconds (6); Application.LoadLevel("Player1"); print("Player 1's Turn"); } }
Regardless of which scene I run the script from, it will print the scene name correctly when I assign it to the string activePlayer, and the if check correctly causes the game to print player 1/player 2's turn depending on which scene is active.
The issue is that I can't seem to get Application.LoadLevel to switch the scenes, as it currently just reloads whichever one is currently active.
You aren't using EditorApplication.currentScene in the game are you? That isn't going to work when it's built - you need to be using loadedLevelName
I just tried out loadedLevelName and it worked perfectly.
Answer by jaskij · Sep 18, 2012 at 07:36 AM
If they're indeed shooting at each other, changing the scene for that seems kinda strange... Shouldn't you rather just switch the camera?
And you have to set up the levels in the build options for them to load through Application.LoadLevel();
They are both shooting, but the way I can really only have 1 castle on screen at once because the bricks in the castle are all rigidbodies that interact with whatever hits them, and 2 castles causes too much lag. I want to set it up so that player 1 takes a shot, then it saves his scene and switches to player 2, who then takes his shot (sort of like battleship). I made sure that both scenes show up in the build options, I'm just not sure why Application.LoadLevel is not actually loading the level I pass in and is just reloading the current scene.
A ton of rigidbodies, and on a V$$anonymous$$ no less... Or are you compiling it to an exe? No easy way out here though, methinks.
Saving the positions and forces on all of those rigidbodies between level switches is going to take forever and you are going to have to use a plugin or write the code for it yourself - Unity does not have that feature built in.
I would suggest that you:
Have both castles in the same scene
Don't attach rigidbodies to start with - only add them when you can identify that they are going to be hit - or sleep them all manually to reduce the lag issues.
Use fewer bricks
For the record. If you're trying to make an "angry birds" -like scene.
That does not actually use PhysX. It uses a 2D physics library, such as Cocos2D or chipmunk.
remarkably I believe someone has ported Cocos2D here to Unity .. just search on the forums. Hope it helps in some way.
Your answer
Follow this Question
Related Questions
time between scenes (loading time) 0 Answers
switch player and npc follow player 0 Answers
How to switch between cameras/players? 1 Answer
Transfer data between scenes + saving it C# 1 Answer
Passing "best" to the other scene 0 Answers