0
\$\begingroup\$

Can anyone help me someone with this code?

I'd like to go to the next level by clicking a button: example my level name is L1,L2,L3,L4,L5,L6,L7 and when I finish level L1, I want to go to level L2 with the click of a button, and from L5 go to L6 like this.

public class GameLevels : MonoBehaviour {

 public Button Play;
 private int idLevel;
 // Use this for initialization
 void Start () 
 {
     idLevel = PlayerPrefs.GetInt("idLevel");
     Play.interactable = false;
 }
 public void SelectLevel(int i){
     idLevel = i;
     PlayerPrefs.SetInt ("idLevel", idLevel);
     Play.interactable = true;
 }

 public void LoadLevel(string Levels)
 {
     SceneManager.LoadScene (Levels);
 }
 public void Replay()
 {
     SceneManager.LoadScene ("L" + idLevel.ToString ());
 }
 public void loadNextLevel() 
 {

 }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ What is the problem? \$\endgroup\$ – jgallant Dec 2 '16 at 14:37
  • \$\begingroup\$ What is it you are looking for? How to get the button input? \$\endgroup\$ – realUser404 Dec 2 '16 at 14:44
  • \$\begingroup\$ Problem is i dont know how to input the next level code for example if i finish level 1 "L1" with press button to go in level 2 "L2" and if i finish level 2 "L2" with press again that button to go level 3 "L3". \$\endgroup\$ – Driton Dec 2 '16 at 15:01
  • \$\begingroup\$ Don't you have the "idLevel"? Why something like SceneManager.LoadScene ("L" + (idLevel + 1).ToString ());does not work? \$\endgroup\$ – realUser404 Dec 2 '16 at 15:22
1
\$\begingroup\$

If this is the Replay button and it works (As we don't have the full code, I'm not sure if it works but I'm assuming it does):

public void Replay()
{
    SceneManager.LoadScene ("L" + idLevel.ToString ());
}

Then it stands to reason that the following should be the next level's code:

public void loadNextLevel() 
{
PlayerPrefs.SetInt("idLevel", idLevel+1);
SceneManager.LoadScene ("L" + (idLevel+1).ToString ());
}

Since idLevel is declared as an int, you can just add 1 and then turn it into a string.

You can also do this (when you load the next level, idLevel will be reset if I'm not mistaken, so it should be safe to do it this way too):

public void loadNextLevel() 
{
idLevel++;
PlayerPrefs.SetInt("idLevel", idLevel);
SceneManager.LoadScene ("L" + idLevel.ToString ());
}

P.S. You shouldn't have to turn it into a string because when you add an int to a string, it will also act as a string. Just make sure you do the math before the string addition, so don't forget to use the parentheses where we do (idLevel+1). So, SceneManager.LoadScene ("L" + (idLevel+1)); should also work.

\$\endgroup\$
  • \$\begingroup\$ Hello this dont work because is load L2 but if i finish L2 dont load L3 but back me again in L2 and same load L2,L2,L2. \$\endgroup\$ – Driton Dec 2 '16 at 20:16
  • \$\begingroup\$ Did you remember to set the PlayerPrefs variable you're taking this value from? I will make necessary edits in the last part (in a minute), use that code instead. \$\endgroup\$ – John Hamilton Dec 3 '16 at 11:40
  • \$\begingroup\$ Thank you so much sir i solved the problem public void loadNextLevel() { PlayerPrefs.SetInt("idLevel", idLevel+1); SceneManager.LoadScene ("L" + (idLevel+1).ToString ()); } thi code is correct code :D \$\endgroup\$ – Driton Dec 3 '16 at 13:12
  • \$\begingroup\$ i created the main menu screen and game play in same scene. if player lost in game over page it ll show a button replay. if i hit replay the game wants to start directly. it don't want to show main menu screen again. how can i do that? \$\endgroup\$ – Manoj Balaji M Sep 19 '17 at 13:43
  • \$\begingroup\$ @ManojBalajiM You could just make a global variable, public static bool levelRestart = false; then set that to true when the player pushes the replay button. Then check whether the level is a restart or not in the menu's Start() function and close the menu if it is then set the variable to false after that. Remember that to access a global variable, you need to specify the script's name and then access it, i.e. GlobalVars.levelRestart and do keep in mind globals are somewhat volatile, since they can be accessed from anywhere. Though you should really ask this as a separate question. \$\endgroup\$ – John Hamilton Sep 20 '17 at 5:30
2
\$\begingroup\$

try the simple code. it ll solve your problem

//to reload the Level

public void Replay(){
    Application.LoadLevel (Application.loadedLevel);
}

//To load next level

public void NxtLevel(){
    Application.LoadLevel (Application.loadedLevel + 1);
}

this ll load the level by using level id. Located in build settings -> Scenes in build.

\$\endgroup\$
-4
\$\begingroup\$

Its may look like this :

 public class GameLevels : MonoBehaviour {

 public Button Play;
 private int idLevel;
 // Use this for initialization
 void Start () 
 {
     idLevel = PlayerPrefs.GetInt("idLevel");
     Play.interactable = false;
 }
 public void SelectLevel(int i){
     idLevel = i;
     PlayerPrefs.SetInt ("idLevel", idLevel);
     Play.interactable = true;
 }

 public void LoadLevel(string Levels)
 {
     SceneManager.LoadScene (Levels);
 }
 public void Replay()
 {
     SceneManager.LoadScene ("L" + idLevel.ToString ());
 }
 public void loadNextLevel() 
 {
  int idLevelSecond = PlayerPrefs.GetInt("idLevel");
  string thelevelnumber = idLevelSecond.ToString();
  string leveltoload = "L" + thelevelnumber;
  Debug.Log(leveltoload);
  SceneManager.LoadScene(leveltoload);
 }
}

UNTESTED ! if the code did not work, tell me.

\$\endgroup\$
  • \$\begingroup\$ Isn't it going to load the same level as before? \$\endgroup\$ – realUser404 Dec 2 '16 at 16:23
  • \$\begingroup\$ i don't know how to increase value in player perfs , editing to add comment so he would add it to himself , wait \$\endgroup\$ – user6668201 Dec 2 '16 at 16:25
  • 1
    \$\begingroup\$ I am not the OP, I am not going to try, I downvoted because I did not find any explanation in the answer, just some unindented code. \$\endgroup\$ – realUser404 Dec 2 '16 at 16:26
  • 2
    \$\begingroup\$ 1- Code without explanation has absolutly 0 interest, even if it is what OP asked. 2- Your answer is bad in several ways. 3- You should learn to accept critism without acting like a 5 years old child. \$\endgroup\$ – realUser404 Dec 2 '16 at 16:48
  • 1
    \$\begingroup\$ Please don't use your answer as a medium to reply to comments. \$\endgroup\$ – Gnemlock Dec 2 '16 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.