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??
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
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.
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?
β Rgalaxy