- Home /
Endless counters in foreach loop
Hey Guys:) Can anyone plz tell me how i can avoid my counters "nbr", "nbr2" and "nbr2" going into somekind of endless loop?? I just want the counters to go up by 1 foreach item added to my list. Here is my code:
//Inventory
private bool _DisplayInventoryWindow = true;
private const int Inventory_Window_ID = 1;
private Rect _InventoryWindowRect = new Rect(10f,10f,300,290f);
private static int _InventoryRows =4;
private static int _InventoryCollums = 3;
private int _InventoryCheck = _InventoryRows * _InventoryCollums;
private bool InvFull = false;
//INV_Window
private int nbr = -1, nbr2 = 0, nbr3 = 0;
public List<GameObject> _LootItems = new List<GameObject> ();
//Button Prop
private float ButtonWidth = 40f;
private float ButtonHeight = 40f;
private float _offset = 1.1f;
void OnGUI()
{
if(_DisplayInventoryWindow == true)
_InventoryWindowRect = GUI.Window (Inventory_Window_ID, _InventoryWindowRect, InventoryWindow, "Inventory");
}
void InventoryWindow (int id)
{
foreach (GameObject go in _LootItems) {
nbr++;
nbr3++;
if (nbr == _InventoryCollums) {
nbr -= _InventoryCollums;
nbr2++;
}
if (_LootItems.Count >= 0)
GUI.Button (new Rect (_offset * (nbr * ButtonWidth), 20 + _offset * (nbr2 * ButtonHeight), ButtonWidth, ButtonHeight), (nbr3).ToString ());
if(nbr3 == _InventoryCheck)
{
InvFull = true;
}
Debug.Log ("nbr:" + nbr);
Debug.Log ("nbr3:" + nbr2);
Debug.Log ("nbr2:" + nbr3);
}
Answer by smoggach · Aug 18, 2014 at 05:11 PM
Your question is confusing. This code will go through each item in _LootItems every single frame that _DisplayInventoryWindow is true.
You never reset nbr, nbr2, and nbr3 to a default value after you're done counting them. This is the problem. You may think it's endlessly looping because OnGUI is an endless loop. I'm thinking you probably only want InventoryWindow to count the nbrs once every time InventoryWindow is initially displayed.
In general doing any sort of calculation in OnGUI does not work.
There are at least two passes of OnGUI every frame. One Layout, and one repaint. If there are any events to process (say the user clicks a button) then there are two more events to process. So in a best case scenario nbr will increase twice per item in _lootItems. And it could fire many more times.
Thats because i haven't got to the remove gameobject part yet. I kinda got stuck while trying to add a gameobject to my list.
I'm aware of OnGUI works like the Update methode, but what i would like to know is, how i make the counter go up by 1 foreach gameobject in the list - only that one time -, ins$$anonymous$$d of looping through and keep adding 1 to the counters for each frame.
If im still confusing, plz do tell me, then i will try to rephrase myself.
Your answer

Follow this Question
Related Questions
A node in a childnode? 1 Answer
Copy values between two classes in two lists. 1 Answer
Problems with Inventory System 1 Answer
C# List foreach problem 1 Answer
Iterating through nulls in legacy-style Animation component 0 Answers