- Home /
 
Scroll List Problem
I want to create an item menu controllable by arrow keys, much so like the one in most Final Fantasy Games and various other JRPGs. So I chose to use a selection grid.

Now, this is currently my OnGUI function:
 function OnGUI()
 {
     scrollPosition = GUI.BeginScrollView(Rect (10,10,220,300),scrollPosition, Rect (0, 0, 220, 400));
 
     for(var i : ConsumableItem in itemBag)
     {
         //Reason: Using a built-in array
         var newBag = new Array(selectionStrings);
         //Add the item to the temp bag
         //var newItemName : String = (i.name + " : " + i.itemAmount);
         newBag.Add(i.name);
         Debug.Log(i.name + " has been picked up");
         //Add the contents of the temporary bag into the main bag
         selectionStrings = newBag.ToBuiltin(String);
     }
     
     selectionGridInt = GUILayout.SelectionGrid (activeSelected, selectionStrings, 1, GUILayout.Width(200));  
     
     GUI.EndScrollView();
 }
 
               The list is to automatically populate as I add new items, (which it appears to be doing in the inspector), however, I can't keep it from duplicating the same entry repeatedly, (and i know the reason for that is because of my loop)
I was able to copy your code into a script and run it without problems (I did translate to C# first), so the issue is likely elsewhere in your code. Can you send us the specific errors you're getting, and under what circumstances they occur?
I tested the very bottom piece of code, the one you said produced errors:
 var selGridInt : int = 0;
 var selStrings : String[] = ["Grid 1", "Grid 2", "Grid 3", "Grid 4"];
 function OnGUI () {
     selGridInt = GUILayout.SelectionGrid (selGridInt, selStrings, 2);
 }
 
                  In addition to C#, I tried it in Javascript and it worked just fine.
Answer by SrBilyon · Jan 06, 2012 at 09:53 PM
Figured it out (how silly of me). I just needed to add the entry of the item in the same function that I add the item to the actual inventory's array.
One array holds the string (stringArray) The other array holds the item (itemArray)
When I add an item to the itemArray, I add a string to the stringArray at the same.
Your answer
 
             Follow this Question
Related Questions
Preventing Items from shifting in a list 1 Answer
Problem with dropping items from my inventory 0 Answers
List array in GUI.Box 0 Answers
How i can make a script-made button interactable? 0 Answers
Why am I getting a null reference? 2 Answers