- Home /
The best way to load scenes ingame
Hi everyone, I was just wondering, what is the best way to load scenes like levels. I dont know how unity loads scenes, but im guessing that theres more to it than just going from one scene to another. At the moment I am using this:
Application.LoadLevel("Scene1");
I seem to be running into problems with this, the game crashes between scene loads from time to time, which will ruin my game, and i can't figure out why. The games not partcuarly big, and when it crashes it freezes my whole computer, and i have to switch the power off, ctrl alt del doesnt even work. If anyone can give me any ideas that would be a massive help :) Thanks alot, Sean
I do not have this problem. I think that Application.LoadLevel(); is the standard way of switching scenes. I presume you're running Windows 7?
To the best of my knowledge that is the best way to load scenes although what you might wanna try is to try loading a scene using it's index ins$$anonymous$$d of its name so for example: ins$$anonymous$$d of
Application.LoadLevel("Scene1");
type
Application.LoadLevel(1); ***that is to say if Scene1 had a build indext of 01. Also try to build and run your game again after make this adjustment.
Answer by DaveA · Sep 09, 2012 at 09:04 PM
That's the way to do it. So now you get to debug. I would start by making your 'next' scene empty. See if you can switch to that empty scene. If so, start migrating game objects one by one from the scene that was crashing into that empty scene, noting when it starts crashing again. The last thing you added will be the culprit and then you have to find out why.
If trying to swtich to an empty scene still crashes, then it's something in the 'destroy' phase of your current scene. Start by disabling game objects one by one until it stops crashing. The last thing you disabled will be the culprit, then you have to find out why.
great solution Dave A....i also ran into the same problem as Supersheep and will try out your solution....thanx
I wish I'd seen your answer before I posted $$anonymous$$e... it is quite excellent! (hadn't even considered that the destroy phase could be a culprit)
Thanks guys, im guessing that it is something in the current scene as this scene crashes when i rotate the scene view now. something in the destroy function must be crashing it during the scene change. Ive tried using both room names and scene index, it doesn't seem to make a diffrence. Could it be a model in the scene causing the game to crash?
It's possible, but usually it's a script. Try disabling gameobjects one by one now, see what happens.
Answer by kristercollin · Sep 09, 2012 at 09:36 PM
Hey Supersheep,
I don't see anything wrong with your use of "Application.LoadLevel()". I use it all the time to load scenes according to their names, but, as Amasiye313 suggested, you can also use the indices.
Maybe you should try setting up a new project with two scenes, each scene having a gameobject inside it that will allow you to go back and forth between the scenes based on input (and nothing more), so that you can have a good comparison between both projects and it'll tell us whether there is problem with the scenes and project that you have currently (maybe they're overloaded in some way).
If you make this new project and the scene switching works, then I'd say the culprit lies elsewhere in your project, but I'll make no guesses until I hear back.
Let us know how it goes, yeah? Good luck!
@kristercollin is " Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().buildIndex +/- 1);" the wrong way for moving to next OR previous scene? I encountered a problem, that if the current level is won,lost,tied. and if we move to main menu scene,and again if I move to the level scene then the state of the level remains the same ie: won,lost,tied etc.
Answer by supersheep · Sep 10, 2012 at 01:21 PM
Possibly fixed it by removing a group of objects that all had ridgidbodies attached i'd used to mke a wall out of individual bricks. The scene it was loading from crashed during the destroy phase i think. Fingers crossed its ok now, but its one of those annoying bugs that pretends to be gone and then comes back for no reason.
Your answer
Follow this Question
Related Questions
Setting variable as type "Scene" 4 Answers
Scene Variable Serialization issues on crash, save etc. 1 Answer
I cant load my scene? 0 Answers
Load a scene/level when enemy is close... 1 Answer
Load 3d models with code, possible? 2 Answers