Scenes ignore index order. Help?

Hi there folks,

currently making a little game for my uni project, I’ve stumbled upon a problem I failed to solve myself (including watching over a dozen videos on the subject). Currently, I’m having two scenes: the startmenu (index 0), and the game level(index 1). So the problem now is when I start the game within the editor, the game skips the start menu and jumps directly into the game. I tried to resolve the problem with a boolean value check, but it resulted in an infinite loop of both the scenes being loaded. I guess it’s just a little detail speck I may have missed, but could you guys help me settle this?

I’m providing you the code for the start menu manager and the game controller.

Start menu:

using UnityEngine;
using System.Collections;

public class StartMenuScript : MonoBehaviour {

	public void endTheGame(){
		Application.Quit ();
	}

	public void startTheGame(){
		Application.LoadLevel (1);
	}
}

Game controller:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class GameController : MonoBehaviour {
	public GameObject[] hazards;
	public int hazardCount; //current count of hazards
	public int hazardMax; //maximum amount of hazards at the same time during respawn
	public int hazardStart; //start amount of hazards

	public float boundaryX = 3f;
	public float boundaryY = 5.5f;
	
	public Text scoreText;
	public Text restartText;
	public Text gameOverText;

	private bool withinCollider;
	private bool restart;
	private bool gameOver;
	private int score;
	
	
	void Start (){
		restart = false;
		gameOver = false;
		scoreText.text = "Score: 0";
		restartText.text = "";
		gameOverText.text = "";
		score = 0;
		UpdateScore ();
		for (int i = 0; i < hazardStart; i++) {
			GameObject hazard = hazards [Random.Range (0, hazards.Length)];
			Instantiate (hazard,
			            new Vector3 (Random.Range (-boundaryX - 1f, boundaryX + 1f),
			            			 Random.Range (-boundaryY - 1f, boundaryY + 1f),
			            			 0), 
			             Quaternion.identity
			);
			if (withinCollider) {
				hazardStart--;
				Debug.Log ("Asteroid innerhalb Safezone entfernt.");
			}
		}
		hazardCount = hazardStart;
		hazardMax = hazardCount;
	}

	void Update (){
		if (restart) {
			if (Input.GetKeyDown (KeyCode.R)) {
				Application.LoadLevel (Application.loadedLevel);
			}
		}
		if(gameOver){
			restart = true;
			restartText.text = "Press 'R' to restart";
		}

		if (hazardCount < 6) {
			for (int j = hazardCount; j < hazardMax; j++) {
				GameObject hazard = hazards [Random.Range (0, hazards.Length)];
				Instantiate (hazard,
				             new Vector3 (Random.Range (-boundaryX - 1f, boundaryX + 1f),
				             Random.Range (-boundaryY - 1f, boundaryY + 1f),
				             0), 
				             Quaternion.identity
				);
				if (withinCollider) {
					hazardStart--;
					Debug.Log ("Asteroid innerhalb Safezone entfernt.");
				}
				hazardCount++;

			}
			hazardMax++;
		}
	}
	
	public void AddScore (int newScoreValue){
		score += newScoreValue;
		UpdateScore ();
	}
	
	void UpdateScore (){
		scoreText.text = "Score: " + score;
	}
	
	public void GameOver (){
		gameOver = true;
		gameOverText.text = "Game Over!";
	}

	void OnCollisionEnter2D(Collision2D col){
		if(col.gameObject.tag == "Enemy"){
			Destroy(col.gameObject);
			withinCollider = true;
		}
	}

}

When you enter playmode in the editor you always start with the scene currently loaded, not with the first scene in the build settings. The editor is not the final game.

If you want to start your game with the menu scene you have two options:

  • load the menu scene before you enter playmode
  • Add a script to the game scene which detects if the menu scene “has already been loaded”.

For the second approach you could use the following two scripts. The “Menu” script should be on a gameobject in the menu scene. “ForceMenuScene” should be added to an gameobject in the game scene(s). If you start your game with the “game” scene the script will detect that the menu scene hasn’t been loaded yet and immediately switches to the menu. Once the menu scene has been loaded the boolean flag will be true and if you switch again to the game scene nothing will happen.

public class Menu : Monobehaviour
{
    private static bool m_MenuInitialited = false;
    void Awake()
    {
        m_MenuInitialited = true;
    }
    public static void StartWithMenu()
    {
        if (!m_MenuInitialited)
        {
            Application.LoadLevel(0);
        }
    }
}


pubic class ForceMenuScene : Monobehaviour
{
    void Awake()
    {
        Menu.StartWithMenu();
    }
}