Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to access/change certain button from GUI.SelectionGrid

Discussion in 'Scripting' started by whitedrow, May 31, 2012.

  1. whitedrow

    whitedrow

    Joined:
    Mar 31, 2008
    Posts:
    15
    Hi,

    I would like to use the GUI.SelectionGrid to build a menu with 6 Buttons.
    They all have the same background image and the same MouseOver background image.
    To do this I use a GUISkin and the following code:
    Code (csharp):
    1. var selGridInt : int = 0;
    2. var selStrings : String[] = ["D4", "D6", "D8", "D10", "D12", "D20"];
    3.     GUI.skin = GUISkinDices;
    4.     selGridInt = GUI.SelectionGrid (Rect (25, 0, 500, 200), selGridInt, selStrings, 3);
    The problem:
    How can I acces or change an individual button?
    And how can every single button have his own icon?
    Let's say I press the "D8" button. I then want o change the text from "D8" to something like "Dice 8 sided" and I want to display a certain Icon on this button.


    I don't know how to access the current active button.
    I thougt I could be something like:
    Code (csharp):
    1. GUI.SelectionGrid[selGridInt].text = "Dice 8 sided"
    But this doesn't work.

    Can anyone help?
     
  2. tonyd

    tonyd

    Joined:
    Jun 2, 2009
    Posts:
    1,224
    The SelectionGrid draws buttons based on the array you pass, which can be an array of strings, textures, or GUIContents.

    Normal usage is:

    Code (csharp):
    1. gridSelection = GUI.SelectionGrid(gridPosition, gridSelection, gridStrings, buttonsPerRow);
    2. if (gridSelection == 1) //do something
    Pass an array of textures instead of an array of strings:

    Code (csharp):
    1. gridSelection = GUI.SelectionGrid(gridPosition, gridSelection, gridTextures, buttonsPerRow);
    And see GUIContent if you want to use both textures and strings.


    If you are using an array of GUIContents named gridContents:

    Code (csharp):
    1. if (gridSelection == 2)
    2.     gridContents[2].text = "Dice 8 sided";
    3.     gridContents[2].image = alternativeTexture;
    You can also access like this:

    Code (csharp):
    1. gridContents[gridSelection].text = "Dice 8 sided"
     
  3. whitedrow

    whitedrow

    Joined:
    Mar 31, 2008
    Posts:
    15
    Hi tonyd,

    thank you for your quick reply.
    I've tried it with a String Array and everything works just fine.
    But when I use an GUIContent Array like this:

    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var gridContents : GUIContent[];
    5.       gridContents = new GUIContent[3];
    6.  
    7. var iconD4  : Texture;
    8. var iconD6  : Texture;
    9. var iconD8  : Texture;
    10. var iconD10 : Texture;
    11. var iconD12 : Texture;
    12. var iconD20 : Texture;
    13.  
    14. var GUISkinDices : GUISkin;
    15.  
    16.  
    17. function Update () {
    18.     gridContents[0].text  = "Dice 4";
    19.     gridContents[0].image = iconD4;
    20.     gridContents[1].text  = "Dice 6";
    21.     gridContents[1].image = iconD6;
    22.     gridContents[2].text  = "Dice 8";
    23.     gridContents[2].image = iconD8;
    24. }
    25.  
    I get some (mostly 2) errors saying:
    NullReferenceException: Object reference not set to an instance of an object
    js_config.Update () (at Assets/js_config.js:29)

    Line 29 is
    Code (csharp):
    1. gridContents[0].text  = "Dice 4";
    I think that the array is generated to slow so that the update() function needs two more cycles.
    What can I do to avoid these errors or do I use the GUIContent the wrong way?
     
  4. tonyd

    tonyd

    Joined:
    Jun 2, 2009
    Posts:
    1,224
    You need to populate your gridContents array.

    Code (csharp):
    1. function Awake(){
    2.     gridContents = new GUIContent[3];
    3.     for (item in gridContents)
    4.         item = new GUIContent();
    5. }
    You could also do it this way:

    Code (csharp):
    1. function Awake(){
    2.     gridContents = [GUIContent(), GUIContent(), GUIContent()]; 
    3. }
    Also, you should probably move the code you have in Update() to Awake() or Start().
     
    Last edited: Jun 1, 2012