- Home /
One player, Many levels
Hello, I am Brand new to unity and I would like to know two things, Is it possible to have More than one scene in a game? If so, How would you do it.
Second: If it is possible, Can I use A Single player for the whole game, or do I have to put a player in every scene?
See, I use Game maker and I can make an object persistent for every level. Is that possible in unity?
Thanks Very much
Answer by IgnoranceIsBliss · Jan 22, 2012 at 11:51 PM
You can make multiple scenes in a single project easily. Try 'New Scene' in the file menu.
You can move between scenes using the Application.LoadLevel("") function. You will have to put these scenes into your final build using 'Build Settings' in your File menu.
You can use the DontDestroyOnLoad (http://unity3d.com/support/documentation/ScriptReference/Object.DontDestroyOnLoad.html) function on an object to prevent Unity from removing it when you move to other scenes.
Normally you would create an 'Empty' object in your first level, give it a behaviour that stores all of the player information in static members, and make sure it marks itself as DontDestroyOnLoad during the 'awake' or 'start' function. You can then use it as a persistent store of all of your player details and scoring.
Answer by syclamoth · Jan 22, 2012 at 11:47 PM
First: You just go to the 'file' menu, and use 'new scene'! If you want to go between different scenes during the game, use
Application.LoadLevel(int);
where int is the index of the level. This index is set up in the build settings, which will automatically pop up if you go to create a build of the game.
Second: Yes, you can use a persistent object over scenes. What I usually do in these situations is put persistent objects in a 'setup-void' scene with only a few, minimal objects in it, and call
DontDestroyOnLoad(gameObject);
on each of the gameObjects that I want to keep. I then load the first level, but keep all the characters and game data from the 'void' level. The reason for this is so that I can move freely between the actual levels, without worrying about the game spawning a new GameEngine and Player if I happen to go back to the start.
Basically, you can easily make an object persistent between levels, using DontDestroyOnLoad. It's not the only way to manage this kind of behaviour, but it's the way that makes the most sense.
You can also load a level by name if you want - use a string ins$$anonymous$$d of an int in your call to LoadLevel.
I find that this is a lot clearer to work with.
I hate doing that. String literals just rub me the wrong way.
I agree with the sentiment - but when it's an arbitrary order that may change at any time, it can be awkward. You're unlikely to change the name of a scene - and in fact, you can't do it from Unity at all (although you can do it from the file system).
Of course, if you plan on using different scenes for different devices, it makes a lot of sense to use integers rather than strings. I suppose it's use-dependent.
Answer by RMsoftware1 · Jan 22, 2012 at 11:58 PM
Ok, Makes sense,just curious, is this Javascript or C#
Who are you talking to? Please post comments in the 'add new comment' section of the relevant post.
And to answer your question- Yes, it is Javascript or C#. All of the code that has been posted here will work equally well in both languages. In future, if you have more specific questions, it's nice if you specify what language you want, either in a tag or in your question text. That way we can help you more easily!
Your answer
![](https://koobas.hobune.stream/wayback/20220613063300im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Stop Time for Specific Objects? 4 Answers
fade object out? 1 Answer
Is there a way to shake an Object fast? 1 Answer
What should I change in this instantiating a GameObject in a script using Unity? 1 Answer