Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Jepz · Aug 18, 2014 at 03:30 PM · listforeach

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);
     
                             }









Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kiwasi · Aug 18, 2014 at 07:26 PM 0
Share

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.

avatar image Jepz · Aug 18, 2014 at 07:36 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges