- Home /
level selection using gui.SelectionGrid
Hi, I'm trying to create a level menu where i use a grid of button to represent the level and when i click on them it should load the particular level this is the code i'm using using UnityEngine; using System.Collections;
public class MenuScreen : MonoBehaviour{
public string selectedButton = "";
public int selectionGridInt;
public string[] selectionStrings = new string[] { "Level 1", "Level 2", "Level 3", "Level 4", "Level 5", "Level 6", "Level 7"};
void OnGUI()
{
GUILayout.BeginArea(new Rect(350, 320, Screen.width / 3 - 20, Screen.height * 2 / 3));
selectionGridInt = GUILayout.SelectionGrid(selectionGridInt,selectionStrings,2);
GUILayout.EndArea();
selectedButton = selectionStrings[selectionGridInt];
//Application.LoadLevel(selectedButton);
GUI.Label(new Rect(300,10,150,40), selectedButton);
}
}
The problem is when the menu loads up the value of selectionGridInt is always 0 and therefore it loads up the first level by default instantly. There is something i'm obviously doing wrong but can't figure out.. Could someone help me out?
Answer by CorruptedHeart · Feb 06, 2012 at 10:10 AM
One possibility is to have a separate button that then loads the level based on the index.
if(GUI.Button(new Rect(300, 10, 150, 40), selectionStrings[selectionGridInt]))
{
Application.LoadLevel(selectionStrings[selectionGridInt]);
}
Another possibility is to have the first selection grid button to be "None" then just ensuring that if the selectionStrings[selectionGridInt] is None don't load the level
Answer by d.V.b · Feb 12, 2012 at 01:20 PM
I think you can initialize selectionGridInt to -1 in Start(). Then add an if(selectionGridInt > -1) to load your level :)
Answer by efge · Sep 02, 2012 at 01:18 PM
You could also set the selectionGridInt in Start() to a value higher than your max. buttons. Something like this:
void Start()
{
selectionGridInt = selectionStrings.Length;
}
Your answer