The question is answered, right answer was accepted
Help to Stop Level Load Loop
Hi All,
I have just recently added to my level manager script so that once max level points are achieved on level_1 load level_2 and continue points to level_2 from level_1.
The problem is that when level_2 loads the points are shown from Level_1 but the level wont play, i know that the level is continuously loading but i am stuck on how i can stop it loading so i can play the next level?
my script is:
using System.Collections;
public class LevelManager : MonoBehaviour {
public LevelManager levelManager;
public int score;
public int Score=30;
private PlayerController player;
// Use this for initialization
void Start () {
player = FindObjectOfType<PlayerController>();
if (Application.loadedLevelName == "Level_1") {
score = 0;
saveScores();
} else {
score = getSavedScores();
}
}
// Update is called once per frame void Update ()
{
saveScores();
changeLevel();
}
public void changeLevel()
{
if (score >= 750)
{
saveScores();
Application.LoadLevel("Level_2"); // save score before level loads & loads level
}
if(Application.loadedLevelName == "Level_2") //recognized the name of the level loaded & access score
{
getSavedScores();
}
}
//possible if the save functionality intreacts with this
public void saveScores()
{
if (score >= 750)
{
// setting the score
PlayerPrefs.SetInt("Score", score);
}
}
public int getSavedScores()
{
int tempScore = PlayerPrefs.GetInt("Score");
return tempScore;
}
public int getScore()
{
return score;
}
public void AddPoints(int points)
{
score += points;
}
void OnGUI()
{
GUILayout.Label(score.ToString());
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.name == "Player")
{
Application.LoadLevel(0); //this is on death completely restart game
}
}
}
Answer by BrodyDaChappy · Jan 30, 2016 at 09:13 PM
Anyone? i have tried as far as my knowledge of C# goes.
Answer by aferlane · Jan 30, 2016 at 09:50 PM
Try tracking the level that you're on as well, something along the lines of this:
public void changeLevel()
{
if (score >= 750 && currentLevel == 1)
{
saveScores();
currentLevel++;
Application.LoadLevel("Level_2"); // save score before level loads & loads level
}
I'm not the greatest scripter, so might need to make some adjustments, but that general idea should help you out.
Thanks Aferlane,
That worked, but i have noticed that when i gain more points in level 2 it still loads level_2, even though i have said if score >= 750 load level 2, so once i gain more points it should stop the load level.
Any ideas?