Search Unity

Created objects in List being deallocated

Discussion in 'Scripting' started by deraggi, Dec 9, 2017.

  1. deraggi

    deraggi

    Joined:
    Apr 29, 2016
    Posts:
    88
    Hi,

    I have a List

    Code (CSharp):
    1. public List<VC_VirtualGood> wonVirtualGoodsList = new List<VC_VirtualGood>();
    I'm adding non-Monobehavior objects to that List with a custom constructor

    Code (CSharp):
    1. wonVirtualGoodsList.Add(new VC_VirtualGood(entry["shortCode"], entry["tag"], entry["name"]));
    My issue is that in the next function, the list only contains null entries. So I assume the objects were deallocated by GC. Any idea how I can fix this?
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    If they show null, it's because they don't point to anything anymore. Usually it's because it's not in the same scope, so something that was accessible in the function, isn't accessible outside it. That's assuming it wasn't null inside the function.
     
  3. deraggi

    deraggi

    Joined:
    Apr 29, 2016
    Posts:
    88
    EDIT: My mistake! The call
    Code (CSharp):
    1. new VC_VirtualGood(entry["shortCode"], entry["tag"], entry["name"])
    returns null.

    But I don't understand why?

    Code (CSharp):
    1. public VC_VirtualGood(string shortCodeToSet, string tag, string nameToSet) {
    2.      
    3.         shortCode = shortCodeToSet;
    4.  
    5.         switch (tag) {
    6.             case "BANNER_BG":
    7.                 type = virtalGoodType.banner;
    8.                 break;
    9.             case "BANNER_LOGO":
    10.                 type = virtalGoodType.logo;
    11.                 break;
    12.         }
    13.  
    14.         vgTextureAsset = VC_SceneManager.Instance.loadoutManager.getTextureFromAssetBundle(shortCodeToSet, type);
    15.  
    16.         vgName = nameToSet;
    17.  
    18.     }
     
    Last edited: Dec 9, 2017
  4. deraggi

    deraggi

    Joined:
    Apr 29, 2016
    Posts:
    88
    Nevermind, got it worked out - my custom class was still inheriting from Monobehaviour