[Closed]Do i need to make LoadLevel script for everytime i want to change scene?

Hey, im new in this community, and trying to learn about unity and creating a simple game based learning game,

and i want to ask is that, should i create script for every time i want to change scene?

for example,

scene 1 β†’ scene 2 [i create 1 javascript with function Application.LoadLevel]

scene 2 β†’ scene 3 [create another javascript with function Application.LoadLevel for scene 3]

Thanks for the answer :slight_smile:

2 Answers

2

Try to find ways where you can reuse as much as possible. If you have a huge amount of scenes which all are presented in chronological order then create a function which you can call from your game when conditions are met for a level load. Overloads are handy as well.

// Automatically increment and loop on exceed
static function LoadLevel () {
    var level : int = Application.loadedLevel+1; 
    level = level%Application.levelCount-1;
    Application.LoadLevel(level);
}

// Load level by string
static function LoadLevel (level : String) {
    Application.LoadLevel(level);
}

// Load level by int
static function LoadLevel (level : int) {
    Application.LoadLevel(level);
}

So calling ClassName.LoadLevel(); would load next level in your listed scenes. Thanks to the overload (a bit redundant in the example), you can also call ClassName.LoadLevel(β€œMenu”) to load up a scene called Menu. Before a level load happens you can execute anything you want, for instance show a summary and outro. What’s beautiful in this case is also that if you decide to change the flow of a scene ending you’ll have everything in one place instead of scattered scripts for each scene.

Functions are the solution for many in other cases repeated tasks. If something will happen repeatedly, try to make it a general solution which you can access from anywhere in your workflow.

i mean, if i have 2 object which one goes to level 1 and the other goes to level 2, should i make 2 script and put script1 in object 1 and script 2 in object 2?

–

Are you asking whether LoadLevel has to be in a special script? (Maybe because of an example in the docs?)

LoadLevel is just a normal command. You can put it anywhere. For example, if the playerScript sees you’ve died, you could call LoadLevel right there, to restart.

Nope, i mean yeha LoadLevel can be put everywhere, but i mean, for example i have 2 button, when pressed button A change scene to mainmenu, and button B change scene to optionmenu.. should i make 2 script, script 1 is application.loadlevel("mainmenu"); and drag it to button A script 2 is application.loadlevel("optionmenu"); and drag it to button B? or i could do something so my script wont flood on assets if i have so much scene changing??

–

Scenes for options menus is probably not a good idea in my opinion. Perhaps for the Start screen which incorporates some options it is ok. Perhaps utitlise [this][1] function? [1]: http://docs.unity3d.com/Documentation/ScriptReference/Application.LoadLevelAdditive.html

–

If loading a scene is situation dependent, like clicking on certain objects in scene then separate scripts is probably a good idea. If you had a tetris game or something like that you could place 1 loading script on to an object and use [DontDestroyOnLoad][1] to make that object persist through a loading without being destroyed. [1]: http://docs.unity3d.com/Documentation/ScriptReference/Object.DontDestroyOnLoad.html

–

Thanks then for the answer, i think i will just go with by creating it 1 by 1. Thanks, meat5000.

–

As you get better at programming, esp. using Unity's public variables, you'll see how to reuse scripts that do about same thing, but different (like going to level X when clicked, used for every scene-click box in the game.) For buttons doing things, you don't usually make one button with a script. For example, the player script can also make and handle all the buttons.

–