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

C# List does not update properly

Discussion in 'Scripting' started by Hibbey, Oct 12, 2014.

  1. Hibbey

    Hibbey

    Joined:
    Jun 1, 2014
    Posts:
    2
    Hi,
    I'm currently creating an inventoryscript which is a list of all children of the inventory gameObject which have the itemscript component attached to them. This function is always called when there are changes to the inventory, in my case when a bullet is loaded into the rifle; however this list does not instantly update after an gameObject (bullet) is destroyed rather that the function must be called an additional time.

    Gunscript - calling to load the bullet

    Code (csharp):
    1.        if(Input.GetButtonDown("Insert Bullet")){
    2.          WeaponHasAmmo();
    3.          if (reloadtype == ReloadType.innermag && inventoryhasbullets == true && innermagsize>currentbulletsinnermag){
    4.            if (leveraction == true){
    5.              if(straightreloadprocess >= 1f || rotreloadprocess >= 1f){
    6.                ++currentbulletsinnermag;
    7.              }
    8.            }else{
    9.              ++currentbulletsinnermag;
    10.            }
    11.            InsertBulletInnerMag();
    12.          }
    13.        }
    Gunscript checking if bullets and space are available, therefore calling functions in inventory script

    Code (csharp):
    1. void InsertBulletInnerMag(){
    2.      if(inventoryhasbullets == true){
    3.        invscript.RemoveAmmofromInv(blltitscp, true);
    4.      }else{
    5.        return;
    6.      }
    7.      WeaponHasAmmo();
    8.    }
    9.  
    10. void WeaponHasAmmo(){
    11.      displayammo = 0;
    12.      if (gunequipped == true){
    13.        invscript.InventoryDisplayConstruction();
    14.        foreach (itemscript i in invscript.displayinvcont) {
    15.          if (i.displayname == blltitscp.displayname){
    16.            displayammo += 1;
    17.          }
    18.        }
    19.        if(displayammo > 0){
    20.          inventoryhasbullets = true;
    21.        }else{
    22.          inventoryhasbullets = false;
    23.        }
    24.      }else{
    25.        return;
    26.      }
    27.    }
    Inventoryscript processing the request

    Code (csharp):
    1.  public List<itemscript> displayinvcont = new List<itemscript> ();
    2.  
    3.    public void InventoryDisplayConstruction(){
    4.      displayinvcont.Clear ();
    5.      displayinvcont = invobj.GetComponentsInChildren<itemscript> ().ToList();
    6.    }
    7.    public void RemoveAmmofromInv(itemscript iscp, bool reloadrequest){
    8.      if (reloadrequest == true){
    9.        for (int i = 0; i<displayinvcont.Count; i++){
    10.          if(iscp.displayname == displayinvcont[i].displayname){
    11.            DestroyAmmoObj(i);
    12.            break;
    13.          }
    14.        }
    15.        reloadrequest = false;
    16.      }else{
    17.        return;
    18.      }
    19.    }
    20.    void DestroyAmmoObj(int i){
    21.      Destroy(displayinvcont[i].gameObject);
    22.      InventoryDisplayConstruction ();
    23.    }
    I tried to call the InventoryDisplayConstruction() function on numerous occasions used it directly before the break, but no matter what I do the list does not update after the displayinvcont[.i].gameObject is destroyed. Does anyone have an idea what's wrong there or is this a problem of lists in general?

    Thanks in advance!
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    There are no problems with list

    http://docs.unity3d.com/ScriptReference/Object.Destroy.html
    "Actual object destruction is always delayed until after the current Update loop, but will always be done before rendering."

    Destroy doesn't actually destroy your object, so when you run the collection, it's still being picked up.
    Why not just use list.Remove(theThing) to change the contents of the list?
     
    Hibbey likes this.
  3. Hibbey

    Hibbey

    Joined:
    Jun 1, 2014
    Posts:
    2
    Thanks for your help, I had completely read over the delay fact, I now added a simple bool in the gunscripts update function to get a correct update of my list. Now everything is working as intended :)
     
    jhonataneng2332 likes this.
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Or just use remove to get item out of list?
    Repeatedly clear'ing and then GetComponentsInChildren'ing is just unnecessary cost