Hello, I’m new to the forums and to Unity, I am having a what I think would be a simple problem.
I have a LevelSelector class that handles the loading of various levels. In the class I would like to have an array of GameScenes / levels that are all found in a certain folder under Scenes\GameLevels
Currently my code is able to load certain levels, but it’s hard coded by name so if I ever add any more levels, I would have to constantly go back and add or remove the couple lines of code that loads that level.
Here is my code so far:
using UnityEngine;
using System.Collections;
public class LevelSelector : ButtonManagerBehavior
{
/// <summary>
/// Number of levels in the Application
/// </summary>
static int levelCount = Application.levelCount;
/// <summary>
/// Array that holds the levels from GameLevels
/// </summary>
GameScene[] levelArray = new GameScene[levelCount];
/// <summary>
/// Activate the button that will be pressed.
/// </summary>
/// <param name='buttonName'>
/// Name of the button in the scene being pressed.
/// </param>
public override void ButtonPressed (string buttonName)
{
/// Switch statement that loads the approiate level based on the level name passed in.
switch(buttonName)
{
/// Load level03 scene
case "level3Button":
Application.LoadLevel("level03");
break;
/// Load level04 scene
case "level4Button":
Application.LoadLevel("level04");
break;
/// Load level05 scene
case "level5Button":
Application.LoadLevel("level05");
break;
/// Go back to the main menu
case "backToMenu":
Application.LoadLevel("MainMenuGUITest");
break;
default:
/// Attempt to load level scene with the same name as the buttonName param passed in
base.ButtonPressed(buttonName);
break;
}
}
}
As you can see I haven’t gotten very far. The switch statement is there to let me load the levels by their name, but I would like to have the levels in the GameLevels folder in the array levelArray.
Any help or insight will be very much appreciated! Thank you!