- Home /
Problem is not reproducible or outdated
[Solved]Application.LoadLevel("Scene"); not working!
The scenes simply dont change Here is my code:
using UnityEngine; using System.Collections;
public class LevelController : MonoBehaviour {
public static LevelController instance;
public int xpMultiply = 1;
public float xpFirstLevel = 100;
public float difficultyFactor = 1.5f;
// Use this for initialization
void Start () {
Application.LoadLevel ("1");
instance = this;
DontDestroyOnLoad (gameObject);
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.A))
AddXp (100);
if (Input.GetKeyDown (KeyCode.R))
PlayerPrefs.DeleteAll();
}
public static float GetCurrentXp() {
return PlayerPrefs.GetFloat ("currentXp");
}
public static void AddXp(float xpAdd) {
float newXp = (GetCurrentXp() + xpAdd)*LevelController.instance.xpMultiply;
if (newXp >= GetNextXp()) {
AddLevel ();
newXp = 0;
}
PlayerPrefs.SetFloat ("currentXp", newXp);
}
public static int GetCurrentLevel() {
return PlayerPrefs.GetInt ("currentLevel");
}
public static void AddLevel() {
int newLevel = GetCurrentLevel() + 1;
PlayerPrefs.SetInt ("currentLevel", newLevel);
}
void OnGUI(){
GUI.Label (new Rect(0, 0, 200, 50), "Current XP = " + GetCurrentXp());
GUI.Label (new Rect(0, 70, 200, 50), "Current XP = " + GetCurrentLevel());
GUI.Label (new Rect(0, 140, 200, 50), "Current Next Xp = " + GetNextXp());
} public static float GetNextXp() { return LevelController.instance.xpFirstLevel GetCurrentLevel()+1 LevelController.instance.difficultyFactor;; }
}
I solved the problem! I forget to put the PlayerStatsController Object in the Loader scene :( Thank You for the attention!
Answer by smnerat · Sep 27, 2014 at 09:39 PM
If your levels have been assigned in the build settings, then I'm pretty sure you just need to get rid of the parenthesis around 1.
Application.Loadlevel(1);
I solve the problem! I forget to put the PlayerStatsController Object in the Loader scene :( Thank You for the attention!
Answer by Bunny83 · Sep 27, 2014 at 09:43 PM
Are you sure that you have added your desired level to the build list and that your level is actually named "1"? Maybe you want to use an index instead of a level name. In that case don't use a string but an integer value.
If you have all your levels / scenes added to that list, do you get any error in the console? "not working" doesn't really help to understand your problem...
I solve the problem! I forget to put the PlayerStatsController Object in the Loader scene :( Thank You for the attention!