x


how to change a gui button texture

I have an inventory script that creates an a button at the bottom of the screen for the items in the inventory, but I am encountering an issue as it currently simply has text, where as I want a different image in each box.

I have linked the script below to show my issue:

pragma strict

var obj_wood : GameObject;

var obj_claw : GameObject;

var obj_bread : GameObject;

var obj_water : GameObject;

var numberOfItems : Number;

public var items = new System.Collections.Generic.Dictionary.<String,GameObject>();

private var curItem : System.Collections.Generic.KeyValuePair.<String, GameObject>;

public var buttonHeight = 50;

public var width = 100;

AddItemToInventory(obj_wood);

AddItemToInventory(obj_claw);

AddItemToInventory(obj_bread);

AddItemToInventory(obj_water);

function AddItemToInventory (item : GameObject) {

 items.Add(item.name, item);

 Destroy(item);

}

function OnGUI() { var curIndex = 0;

    GUI.BeginGroup (new Rect (400, 300, 400, 120));

for(var item : System.Collections.Generic.KeyValuePair.<String, GameObject> in items) {

         var rect : Rect = new Rect((width+2) * curIndex, 0, width, buttonHeight);

         var itemTexture : Texture = "icon_"+item.key;

//i have called each texture icon_"item name", but the texture variable wont accept a string

         if(GUI.Button(rect, itemTexture)) {
              curItem = item;

              RemoveItem(item);
         }
         curIndex++;
    }

    GUI.EndGroup();

}

function RemoveItem (item : System.Collections.Generic.KeyValuePair.<String, GameObject>) {

 yield WaitForEndOfFrame;

 items.Remove(item.Key);

}

function RemoveItem (item : GameObject) {

 yield WaitForEndOfFrame;

 items.Remove(item.name);

}


if you have any idea of how too this it would be appreciated thank you

more ▼

asked Apr 09 '12 at 10:48 PM

chris gough gravatar image

chris gough
97 5 7 9

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first
more ▼

answered Apr 10 '12 at 01:11 AM

Aydan gravatar image

Aydan
180 28 37 47

thanks found the solution through GUI skins

Apr 11 '12 at 01:25 PM chris gough
(comments are locked)
10|3000 characters needed characters left
Your answer
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

By RSS:

Answers

Answers and Comments

Topics:

x3672
x2190
x786
x418
x223

asked: Apr 09 '12 at 10:48 PM

Seen: 1123 times

Last Updated: Apr 11 '12 at 01:25 PM