Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Instruction keeps looping for each entity in List, only want it done once

Discussion in 'Scripting' started by DarkEcho, Sep 6, 2014.

  1. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    231
    When I click a button (Line 11-14) in the GUI (Inventory Window) it is supposed to go to the 'Use()' Function (Line 15-18) in the 'Item' script held by the List Inventory which just debugs the 'itemName' variable in the 'Item' script. This is only done one for that clicked item alone.

    However it does this for all items in the inventory List due to the 'cnt' variable & loop for the creation of the GUI (Bare in mind the inventory might be resizeable in future hence why i didnt do x amount of buttons seperately).

    I know WHY its looping, i need lines 15-18 to do this ONCE for the clicked Button, not looping for every entity in the List

    I was following bergzerg arcades example, although he did it with a double click, i only want a single click. I cant seem to find a pattern in his i need. Heres his script at the correct time. (PAUSE VIDEO IMMEDIATELY, dont worry about the line hes typing)

    Sorry for the messy code, Unity forums fault...
    Code (CSharp):
    1. public void InventoryWindowWieght(int id)    {
    2.  
    3.         int cnt = 0;
    4.  
    5.         for (int y = 0; y < invRows; y++) //Loops for each row of inventory        invRows= 7        (Slot based: invRows to PlayerInventory.Inventory.Count)
    6.         {
    7.             for(int x = 0; x < invColumns; x++) //Loops for each Column of inventory     invColumns= 4  (Slot based: invColumns to 1)
    8.             {
    9.                 if (cnt < PlayerInventory.Inventory.Count) //Variable 'cnt' increases until met with count of items in inventory. Note this is being looped, See above.
    10.                 {
    11.                     if (GUI.Button(new Rect(5 + (x * invButtonWidth), 20 + (y * invButtonHeight), invButtonWidth, invButtonHeight), new GUIContent(PlayerInventory.Inventory[cnt].InvItemIcon, PlayerInventory.Inventory[cnt].ItemName))); //Button Height & Width at 40
    12.                     {
    13.                         GUI.Label(new Rect(0, 40, 100, 40), GUI.tooltip);
    14.                         if(Input.GetMouseButtonUp(0)) {
    15.                             Item selectedItem = PlayerInventory.Inventory[cnt];
    16.                             Debug.Log("InventoryGUI : " + selectedItem.ItemName);
    17.                             Debug.Log("InventoryGUI : left click (Get ButtonUP)");
    18.                             selectedItem.Use();
    19.                         }
    20. }
    21.                     }
    22.                 }
    23.     cnt++;
    24.             }
    25.         }
    26. }
    27.     }
    Cookie2
     
  2. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    I don't understand your code here... the variable cnt is ALWAYS set to zero... so this would loop infinitely unless you're removing items from your Inventory array.
     
  3. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Yeah, blame the forum for not lining up your brackets properly. :p

    Shouldn't "cnt" be inside the inner "for"? Right now, it is outside, so it increment by one for each rows.

    You also have a ; at the end of your "if"...

    Code (CSharp):
    1.     public void InventoryWindowWieght(int id)
    2.     {
    3.         int cnt = 0;
    4.  
    5.         for (int y = 0; y < invRows; y++)
    6.         {
    7.             for (int x = 0; x < invColumns; x++)
    8.             {
    9.                 if (cnt < PlayerInventory.Inventory.Count)
    10.                 {
    11.                     Item selectedItem = PlayerInventory.Inventory[cnt];
    12.  
    13.                     int left = 5 + (x * invButtonWidth);
    14.                     int top = 20 + (y * invButtonHeight);
    15.                     GUIContent content = new GUIContent(selectedItem.InvItemIcon, selectedItem.ItemName);
    16.  
    17.                     if (GUI.Button(new Rect(left, top, invButtonWidth, invButtonHeight), content))
    18.                     {
    19.                         GUI.Label(new Rect(0, 40, 100, 40), GUI.tooltip);
    20.  
    21.                         // This will probably fail.
    22.                         if (Input.GetMouseButtonUp(0))
    23.                         {
    24.                             Debug.Log("InventoryGUI : " + selectedItem.ItemName);
    25.                             Debug.Log("InventoryGUI : left click (Get ButtonUP)");
    26.                             selectedItem.Use();
    27.                         }
    28.                     }
    29.                 }
    30.  
    31.                 cnt++;
    32.             }
    33.         }
    34.     }
    By the way, the Input.GetMouseButtonUp inside a GUI.Button... I think that will never work. Would they ever both return true in the same frame?
     
    Last edited: Sep 6, 2014
  4. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    231
    Whoops! Missed that, daft of me. God knows why that was there. After deleting the ; for some reason it fixed the problem, or something did...do these normally cause errors as Mono & Unity seem to be happy with these, hence why I didn't notice its existence.

    Oh that's for checking if the user is right clicking in the button. (I remove unneeded code not relevant to the question to make it easier on you)
    Code (CSharp):
    1.    if (GUI.Button(new Rect(left, top, invButtonWidth, invButtonHeight), content))
    2.                     {
    3.                         GUI.Label(new Rect(0, 40, 100, 40), GUI.tooltip);
    4.                         // This will probably fail.
    5.                         if (Input.GetMouseButtonUp(0))
    6.                         {
    7.                             Debug.Log("InventoryGUI : " + selectedItem.ItemName);
    8.                             Debug.Log("InventoryGUI : left click (Get ButtonUP)");
    9.                             selectedItem.Use();
    10.                         }
    11. else if(Input.GetMouseButton(1))
    12. Debug.Log("Right Click");
    13. ExternalMenu();
    14. }
     
  5. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906