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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

for loop issue. It must be something obvious...

Discussion in 'Scripting' started by Sajid, Nov 17, 2015.

  1. Sajid

    Sajid

    Joined:
    Mar 12, 2011
    Posts:
    199
    So this snippet of code is intended to add a basic turret object to my inventory.
    Code (CSharp):
    1. public void addBasicTurret()
    2.     {
    3.         invItem basicTurret = new invItem("Basic Turret", basicTurretGameobject, basicTurretSprite, true);
    4.         if (GameControl.GetComponent<Inventory>().isUnique("Basic Turret") == true)
    5.         {
    6.             Debug.Log(GameControl.GetComponent<Inventory>().getEmptyIndex());
    7.             GameControl.GetComponent<Inventory>().addInvItem(GameControl.GetComponent<Inventory>().getEmptyIndex(), basicTurret);
    8.         }
    9.         else
    10.         {
    11.             Debug.Log("we already have this item");
    12.         }
    13.    
    14.     }
    Basically I create an InvItem, then send it to my inventory script in the method called addInvItem() here:
    Code (CSharp):
    1. public void addInvItem(int index, invItem inItem)
    2.     {
    3.         for(int i = 0; i <= invItems.Length; i++)
    4.         {
    5.             if (i == index)
    6.             {
    7.                 invItems[index] = inItem;
    8.                 invHudImages[index].GetComponentInChildren<Image>().sprite = inItem.hudSprite;
    9.  
    10.             }
    11.         }
    12.     }
    . I also have getEmptyIndex() which finds the next open inventory slot and returns its index.
    Code (CSharp):
    1. public int getEmptyIndex()
    2.     {
    3.         for(int i = 0; i <= invItems.Length; i++)
    4.         {
    5.             if(invItems[i].itemName == "")
    6.             {
    7.                 return i;
    8.             }
    9.         }
    10.         return -1;
    11.     }
    However, when I go to my store and buy an item (which calls addBasicTurret), it puts the item in the wrong slot of my inventory array. The inventory is empty, so the item should be added to slot 0, but instead it's being added to the 9th slot in my inventory.

    What's going on here?
     
    Last edited: Nov 17, 2015
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    How is the invItem array initialized? Are you sure gerEmptyIndex returns 0? That string check in there is a bit dodgy. Usually you would test if the array contains null at that index. But if it is a serialized array theres usually the data assigned in the inspector there already.
     
  3. Sajid

    Sajid

    Joined:
    Mar 12, 2011
    Posts:
    199
    getEmptyIndex is working fine, it returns zero when nothing is in the array. It's a serialized array so I can't look for null, otherwise I'd use that instead.

    invItems is initialized like any normal array, containing invItem objects that have things like textures in their constructor.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Out of curiosity, in addInvItem, why are you looping through the array at all? Why not just directly set invItems[index] to what you need?
     
  5. Sajid

    Sajid

    Joined:
    Mar 12, 2011
    Posts:
    199
    Because items might be bought in any order, and they need to go to wherever the first empty slot is.

    I figured it out, I'm so dumb. In my array I accidentally had everything flipped. Everything was working fine, but my UI Image for my 1st inventory slot was actually where 10 should be, ect ect.
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    That explains why you loop in getEmptyIndex, yes. But in addInvItem, you don't put it in the first empty slot. You put it in whatever index is sent to that function. Once getEmptyIndex finds the index, you can just go straight there.
     
  7. Sajid

    Sajid

    Joined:
    Mar 12, 2011
    Posts:
    199
    Wow I didn't even think of that. I fixed it and it cut out a few lines of code. Thanks for pointing that out.