- Home /
Adding Muiltipule GUI.Buttons In an Inventory System Based On Arrays
Is there a way to, from code(JavaScript), to add line of code into my .js file?
I'm working on an inventory system and all my items are identified in an Array, I want to make it so it gets the array length and makes a GUI button for each entry in the array with the corresponding array text for the button text. I also need it so when the button gets clicked it knows what entry in the array needs to be deleted.
I need this answer as soon as possible, I'm trying to finish a system in the next day or two that needs this.
just a side note, for performance management, i really suggest not having a button for each item in your inventory, they are pretty expensive for performance when you have lots on the screen
@Bunnybomb7670 Thanks for the tip, but I'm going to keep doing it with buttons.
@nixcs2512 I don't understand how to use what you wrote. What I want is for there to be a button for every item in the array. And when you click on the button, it will delete the button.
Do you know how to use GUI.Button? If yes, i'm sure that my code will make a button to all items in the array. And i just edit it so you could delete the element in the array by clicking.
Answer by nixcs2512 · Aug 12, 2013 at 09:47 PM
Use a for loop in funtion OnGUI(), you can imagine it would like:
var array : Array[];//your array
function OnGUI()
{
for(var i:int =0;i<array.Length;i++)
{
if(Rect,array[i].text)//Rect is the size of the button,array[i].text is the text in each element of your array
{
array.RemoveAt(i);
}
}
}
It's just the idea, and you need to write and adjust it all by yourself.
Edited it,just a mistake because i'm a C# programmer...
One thing more, maybe this code couldn't run,because with a normal array like int[] or float[] or Gun[] ( Gun is a class non-extended by $$anonymous$$onoBehaviour) you can't use RemoveAt.
Oh no...So you need to read more about GUI.Button, the Rect thing i mention need to be write like Rect(x,y,w,h) with x,y is the coordinate of the rectangle, w is the width and h is height, you can read more in here.
Your answer
Follow this Question
Related Questions
need to shorten my code but unsure of how 3 Answers
Does anyone know of good tutorials for learning arrays and classes? 1 Answer
Inventory Help. 2 Answers
Remove Items and Item Tooltips 0 Answers
Why does it give me this error and how can i fix it? 1 Answer