change the style of a gui by a key press

i am creating an inventory script that i want the user to press the numbers keys to interact with the bar along the bottom, but i cant get the images to change on key press only when they are clicked. i have tried using the toolbar but i get back errors, if someone knows a way in which to set a the toggle to active that would really help.

thanks in advance

function OnGUI() { var curIndex = 0; GUI.skin = customSkin; GUI.BeginGroup (new Rect (400, 500, 400, 120));

     for(var item : System.Collections.Generic.KeyValuePair.<String, GameObject> in items) {
         //iterate through all the items in the dictionary.
          var rect : Rect = new Rect((width+2) * curIndex, 0, width, buttonHeight);
          var customStyle : GUIStyle;
          var btnToggled : boolean;
          var itemIndex : int;
          itemIndex = curIndex;
          customStyle = "icon_"+item.Key;
          GUI.skin = customSkin;
          btnToggled = GUI.Toggle(rect, btnToggled, curIndex+1+")", customStyle);
          //Create a rect for the button.  It is offset more for each item in the array.
          if(Event.current.Equals (Event.KeyboardEvent (curIndex+1+""))) {
                curItem = item;
               //if the user clicks the button, select it as the current item.
          if (curItem.Key == item.Key){
          btnToggled = true;
          //increase the offset counter.



more ▼

asked Apr 12, 2012 at 01:33 PM

avatar image

chris gough
157 18 17 20

A little confused... in your code, customStyle is a GUIStyle object.

But then you say...

customStyle = "icon_"+item.Key;

That line sets it as if it was a string. Wouldn't it be better to say

customStyle = customSkin.GetStyle("icon_"+item.Key);

This gets the named style from your custom skin and then uses it to draw the toggle. As it is, you are using a BLANK custom style, as far as I can tell (and you are doing it before setting the GUI.skin property, too).

Apr 13, 2012 at 05:28 AM IgnoranceIsBliss

Thanks I've changed my script on what you said, but it still didn't solve the issue that I have which is, getting the custom style to change to active when the appropriate key was pressed. so when I press 1, 2, 3 or 4 the image will change to the active state of the style

Apr 13, 2012 at 12:54 PM chris gough
(comments are locked)
10|3000 characters needed characters left

0 answers: sort oldest
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Apr 12, 2012 at 01:33 PM

Seen: 793 times

Last Updated: Apr 13, 2012 at 12:54 PM