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

Objects Disappearing At Runtime?

Discussion in 'WebGL' started by ted-aronson, Jan 13, 2016.

  1. ted-aronson

    ted-aronson

    Joined:
    Dec 28, 2015
    Posts:
    8
    I'm running into a problem where some of my objects are disappearing from collections at runtime. Here's a code example:

    Code (csharp):
    1. public class DemoGrid : IsoGrid
    2. {
    3.   protected List<Farm> _farms = new List<Farm>();
    4.  
    5.   public void Awake()
    6.   {
    7.   List<Crop> gameCrops = CropLibrary.Instance.Crops;
    8.   for (int i = 0; i < gameCrops.Count; i++)
    9.   {
    10.   Crop crop = gameCrops[i];
    11.   _farms.Add(ResourceManager.GetResource<Farm>(crop.FarmPrefab));
    12.   }
    13.  
    14.   StartCoroutine(RefreshGrid());
    15.   }
    16.  
    17.   public IEnumerator RefreshGrid()
    18.   {
    19.   for (int i = (int)MinCoordinates.x; i < (int)MaxCoordinates.x; i++)
    20.   {
    21.   for (int j = (int)MinCoordinates.z; j < (int)MaxCoordinates.z; j++)
    22.   {
    23.  
    24.   Farm farm = Instantiate(_farms[0]);
    25.   Vector3 position = new Vector3(i + ((sizeX - 1) / 2f), 0.5f, j + ((sizeZ - 1) / 2f));
    26.   if (!CanPlaceAt(farm.IsoObject, position))
    27.   {
    28.   Destroy(farm.gameObject);
    29.   continue;
    30.   }
    31.   Collider[] colliders = farm.GetComponentsInChildren<Collider>();
    32.   for (int k = 0; k < colliders.Length; k++)
    33.   {
    34.   Collider c = colliders[k];
    35.   c.enabled = false;
    36.   }
    37.  
    38.   farm.Animator.SetTrigger("DoPlace");
    39.   farm.transform.SetParent(transform);
    40.  
    41.   farm.Animator.SetFloat("GrowthProgress", Random.value);
    42.   AddObjectAt(farm.IsoObject, position);
    43.   }
    44.   }
    45.   }
    46. }
    When I run this code, I eventually get an error for trying to instantiate a null object. It seems that at some point, the first index of _farms becomes null. Problem is, I never set it to null, nor do I ever destroy the object in the List (the Destroy call is on the instantiated version). I can confirm that after the loop in Awake, _farms is populated with Farm objects.

    (I've also seen this behavior where objects in collections become null in other parts of my codebase, but those classes are much larger so I won't post them here.)

    This only seems to happen in WebGL. When I run the game in editor, this script executes without error. The only difference between the editor and WebGL (as far as I can tell) is where I load my AssetBundles from. Has anybody seen similar behavior to this? Could WebGL's GC be out of whack or something?

    *ninja edit for formatting
     
  2. fingersbleeding

    fingersbleeding

    Joined:
    Jun 10, 2013
    Posts:
    24
    Yo Ted,

    I haven't run your code above, but I've been working on a large codebase with webgl for some time now, and haven't experienced losing references to objects stored in collections. We load our views out of resources at runtime, but we package the whole thing together. We don't use asset bundles.

    If you've got the issue broken out into it's own example project, I'd be happy to give it a run through. Hit me up.

    Jason Marziani