Hello all, can someone explain to me the sense of selection grid? I seem to be stucked in here, experimenting about it. What i want to do is have an array of 7 strings, put it in a selection grid then once one of the buttons in the grid is clicked, it will print the corresponding text of that button to a label.
We have a large project that would use that selection grid and this experiment is just a small part of it.
Clicking a button in a selection grid would do a lot of things in the future. But for now, i want to do it in a simple label…
I hope somebody could help me here.
Thanks a lot. 
Well, have a script that checks in Update if the value has changed since the last click. If so, print the new value and update your checking value.
var gridInt : int = 0;
var checkInt : int = 0;
function Update()
{
if (gridInt != checkInt) {
Debug.Log(gridInt);
checkInt = gridInt;
}
}
Hey, thanks for the reply but i can’t get it yet.
Here’s my code so far written in c sharp:
void OnGUI()
{
//player's cards area
Vector2 pos = new Vector2(0, 0);
Vector2 scrollView = Vector2.zero;
int selectionGridInt = 0;
string[] selectionStrings = new string[] { "Card1", "Card2", "Card3", "Card4", "Card5", "Card6", "Card7"};
GUILayout.BeginArea(new Rect(0, 20, Screen.width / 3 - 20, Screen.height * 2 / 3));
selectionGridInt = GUILayout.SelectionGrid(selectionGridInt, selectionStrings, 3, GUILayout.MaxWidth(Screen.width / 3 - 30), GUILayout.MaxHeight(Screen.height * 2 / 3 - 30));
GUILayout.EndArea();
}
void Update()
{
if (gridInt != checkInt)
{
Debug.Log(gridInt);
checkInt = gridInt;
}
}
Would you know by chance how to print the corresponding string when i click a button?
Well, selectionGrid is what you’re using for the selection grid, so gridInt is never being changed. Also, selectionGrid is set to zero every single time OnGUI is run, so it’ll never be anything else. I’d suggest making it a global variable for the script.
if it’s still an open question. This could be a way to do it:
You have the selected button’s index ( meaning selectionGridInt), and since your array of strings is pre defined, use this index to get the string from the array itself.
public class GetTextOfSelectedButton : MonoBehaviour
{
public string selectedButton = "";
public int selectionGridInt;
public string[] selectionStrings = new string[] { "Card1", "Card2", "Card3", "Card4", "Card5", "Card6", "Card7"};
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
// Graphical User Interface. OnGUI is called once per frame
void OnGUI()
{
GUILayout.BeginArea(new Rect(0, 20, Screen.width / 3 - 20, Screen.height * 2 / 3));
selectionGridInt = GUILayout.SelectionGrid(selectionGridInt, selectionStrings, 3, GUILayout.MaxWidth(Screen.width / 3 - 30), GUILayout.MaxHeight(Screen.height * 2 / 3 - 30));
GUILayout.EndArea();
selectedButton = selectionStrings[selectionGridInt];
GUI.Label(new Rect(300,10,150,40), selectedButton);
}
}