I am trying to make a main menu in C# for a unity project. I have some arrays of gameobjects that contain UI elements, divided by location (mm – main menu, songs – song selection, hands – hand selection and powerups – powerups selection).
The function MoveInMenu(int input, GameObject[] menu) takes in the amountof movent in te menu and the array of menu elements that are being navigated. Every array (mm in this case, as it’s the first one to manifest the issue), is populated in the inspector dragging and dropping the various elements.
THe issue this system causes is that when the MoveInMenu() gets called, the game crashes with the following error message: IndexOutOfRangeException: Index was outside the bounds of the array.
With debug tools I discovered that the array menu inside the function is always empty, even when I give it the correct populated array. The whole code of the script follows:
public static MenuUIManager Instance;
[SerializeField] private PlayerScriptableObject player;
public GameObject mainMenuUI, songSelectUI, handSelectionUI, powerupSelectUI; //Schermate
public GameObject[] mm;
public GameObject[] songs;
public GameObject[] hands;
public GameObject[] powerups;
public TextMeshProUGUI info1, info2; //Selezione canzone
public Image albumCover; //selezione canzone
private string activeMenu;
private int positionInMenu = 0;
private int maxMovementInMenu; //Numero di opzioni massime nel menu;
private Song chosenSong;
void Start()
{
Instance = this;
ToMainMenu();
}
void Update()
{
switch (activeMenu)
{
case "Main Menu": //menu principale
{
if (Input.GetButtonDown("Up"))
{
Debug.Log(mm.Length.ToString());
moveInMenu(-1, mm);
}
if (Input.GetButtonDown("Down"))
{
Debug.Log(mm.Length.ToString());
moveInMenu(1, mm);
}
if(Input.GetButtonDown("Submit"))
{
OutOfMainMenu();
}
break;
}
case "Song Selection": //selettore della canzone
{
if (Input.GetButtonDown("Up"))
{
moveInMenu(-1, songs);
UpdateSongSelectionInfo();
}
if (Input.GetButtonDown("Down"))
{
moveInMenu(1, songs);
UpdateSongSelectionInfo();
}
if (Input.GetButtonDown("Submit"))
{
player.noteSpeed = chosenSong.noteSpeed;
ToHandSelection();
}
break;
}
case "Hand Selection":
{
if (Input.GetButtonDown("Up"))
{
moveInMenu(-1, hands);
}
if (Input.GetButtonDown("Down"))
{
moveInMenu(1, hands);
}
if (Input.GetButtonDown("Submit"))
{
OutOfHandSelection();
}
break;
}
case "Powerup Selection": //selettore del powerup
{
if (Input.GetButtonDown("Up"))
{
moveInMenu(-1, powerups);
}
if (Input.GetButtonDown("Down"))
{
moveInMenu(1, powerups);
}
if (Input.GetButtonDown("Submit"))
{
OutOfPowerupSelection();
}
break;
}
case "Tutorial":
{
break;
}
case "Options":
{
break;
}
}
}
public void moveInMenu(int input, GameObject[] menu) //Posizione attuale nel menu, dimensione del menu e direzione del movimento
{
Debug.Log("entered");
positionInMenu += input;
if (positionInMenu < 0) positionInMenu = maxMovementInMenu - 1;
else if (positionInMenu > maxMovementInMenu - 1) positionInMenu = 0;
Debug.Log(positionInMenu);
menu[positionInMenu].transform.localScale = new Vector3 (menu[positionInMenu].transform.localScale.x * 1.2f, menu[positionInMenu].transform.localScale.y * 1.2f, 1f);
menu[positionInMenu - input].transform.localScale = new Vector3(menu[positionInMenu - input].transform.localScale.x / 1.2f, menu[positionInMenu - input].transform.localScale.y / 1.2f, 1f);
}
public void UpdateSongSelectionInfo()
{
chosenSong = MenuMusicManager.Instance.tracklist[positionInMenu].GetComponent<Song>();
albumCover.sprite = chosenSong.albumCover;
info1.text = chosenSong.artist + " - " + chosenSong.title;
info2.text = chosenSong.duration;
MenuMusicManager.Instance.changeMusic(chosenSong.lrBaseSong, chosenSong.lrPianoSong);
}
public void OutOfMainMenu()
{
switch (positionInMenu)
{
case 0:
{
ToSongSelection();
player.isTutorial = false;
break;
}
case 1:
{
ToTutorial();
break;
}
case 2:
{
ToOptions();
break;
}
case 3:
{
ToExit();
break;
}
}
}
public void OutOfHandSelection()
{
switch (positionInMenu)
{
case 0: //R
{
player.midiName = "r" + chosenSong.artist + " - " + chosenSong.title + ".mid";
player.scoreC = chosenSong.rScoreC;
player.scoreB = chosenSong.rScoreB;
player.scoreA = chosenSong.rScoreA;
player.scoreS = chosenSong.rScoreS;
player.baseSong = chosenSong.rBaseSong;
player.pianoSong = chosenSong.rPianoSong;
break;
}
case 1: //L
{
player.midiName = "l" + chosenSong.artist + " - " + chosenSong.title + ".mid";
player.scoreC = chosenSong.lScoreC;
player.scoreB = chosenSong.lScoreB;
player.scoreA = chosenSong.lScoreA;
player.scoreS = chosenSong.lScoreS;
player.baseSong = chosenSong.lBaseSong;
player.pianoSong = chosenSong.lPianoSong;
break;
}
case 2: //LR
{
player.midiName = "lr" + chosenSong.artist + " - " + chosenSong.title + ".mid";
player.scoreC = chosenSong.lrScoreC;
player.scoreB = chosenSong.lrScoreB;
player.scoreA = chosenSong.lrScoreA;
player.scoreS = chosenSong.lrScoreS;
player.baseSong = chosenSong.lrBaseSong;
player.pianoSong = chosenSong.lrPianoSong;
break;
}
}
ToPowerupSelection();
}
public void OutOfPowerupSelection()
{
switch (positionInMenu)
{
case 0:
{
player.powerUp = 1;
break;
}
case 1:
{
player.powerUp = 2;
break;
}
case 2:
{
player.powerUp = 3;
break;
}
case 3:
{
player.powerUp = 0;
break;
}
}
//change scene
}
public void ToSongSelection()
{
mainMenuUI.SetActive(false);
songSelectUI.SetActive(true);
handSelectionUI.SetActive(false);
powerupSelectUI.SetActive(false);
activeMenu = "Song Selection";
positionInMenu = 0;
maxMovementInMenu = MenuMusicManager.Instance.tracklist.Length;
songs[positionInMenu].transform.localScale = new Vector3(songs[positionInMenu].transform.localScale.x * 1.2f, songs[positionInMenu].transform.localScale.y * 1.2f, 1f);
}
public void ToHandSelection()
{
mainMenuUI.SetActive(false);
songSelectUI.SetActive(false);
handSelectionUI.SetActive(true);
powerupSelectUI.SetActive(false);
activeMenu = "Hand Selection";
positionInMenu = 0;
maxMovementInMenu = hands.Length;
hands[positionInMenu].transform.localScale = new Vector3(hands[positionInMenu].transform.localScale.x * 1.2f, hands[positionInMenu].transform.localScale.y * 1.2f, 1f);
}
public void ToPowerupSelection()
{
mainMenuUI.SetActive(false);
songSelectUI.SetActive(false);
handSelectionUI.SetActive(false);
powerupSelectUI.SetActive(true);
activeMenu = "Powerup Selection";
positionInMenu = powerups.Length;
powerups[positionInMenu].transform.localScale = new Vector3(powerups[positionInMenu].transform.localScale.x * 1.2f, powerups[positionInMenu].transform.localScale.y * 1.2f, 1f);
}
public void ToMainMenu()
{
mainMenuUI.SetActive(true);
songSelectUI.SetActive(false);
handSelectionUI.SetActive(false);
powerupSelectUI.SetActive(false);
activeMenu = "Song Selection";
positionInMenu = 0;
maxMovementInMenu = mm.Length;
mm[positionInMenu].transform.localScale = new Vector3(mm[positionInMenu].transform.localScale.x * 1.2f, mm[positionInMenu].transform.localScale.y * 1.2f, 1f);
}
public void ToTutorial()
{
mainMenuUI.SetActive(false);
songSelectUI.SetActive(false);
handSelectionUI.SetActive(false);
powerupSelectUI.SetActive(false);
positionInMenu = 0;
}
}
4