- Home /
Increment doesnt increment after the first increment
I am just trying to use a simple increment to indicate what level to load next after a collision. My levels are named as level 1, level 2 and so on. When I run the game, the initial increment works fine and goes to 2 thus loading my level 2 when I collide. After that, the increment always stays at 2 and doesn't increment to 3,4 and so on. Please advice what I am doing wrong. Thank you.
public int level;
void Start()
{
level = 1;
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Player")
{
if (level < 10)
{
level++;
Application.LoadLevel("level_" + level);
Debug.Log(level);
}
}
}
Answer by Lylek · Jun 29, 2014 at 04:30 AM
If your levels are in order in your Build Settings,
Application.LoadLevel(Application.loadedLevel + 1);
will load the next scene.
Answer by Kiwasi · Jun 29, 2014 at 12:07 AM
If this script is attached to a game object it will be destroyed and recreated with a value of one each time. Two ways to solve.
Make level static
Use DontDestroyOnLoad
There are advantages and problems to both methods.