Example #1
0
    // creates a field item
    public FieldItem CreateItem()
    {
        // sets item values
        GameObject itemBox   = GameObject.Instantiate((GameObject)Resources.Load(itemPrefab));
        FieldItem  fieldItem = itemBox.GetComponent <FieldItem>();

        fieldItem.ResetDespawnCountdown();
        fieldItem.RandomizeItem();

        // returns the new field item.
        return(fieldItem);
    }
Example #2
0
    // gets a field item
    public FieldItem GetItem()
    {
        // generates new item
        if (itemPool.Count == 0)
        {
            // creates the item.
            return(CreateItem());
        }
        else // takes item from the pool
        {
            // original
            // FieldItem itemBox = itemPool.Dequeue();
            // itemBox.ResetDespawnCountdown();
            // itemBox.gameObject.SetActive(true);
            // itemBox.RandomizeItem(); // randomize item

            // new
            FieldItem itemBox = null;

            // checks for null values
            do
            {
                // gets item
                itemBox = itemPool.Dequeue();

                // if the item box was null, go through the list again.
                if (itemBox == null)
                {
                    continue;
                }

                // sets values if it isn't null.
                itemBox.ResetDespawnCountdown();
                itemBox.gameObject.SetActive(true);
                itemBox.RandomizeItem();                     // randomize item
            }while (itemBox == null && itemPool.Count != 0); // while a preset item hasn't been found.

            // no items were found in the list.
            if (itemBox == null)
            {
                itemBox = CreateItem();
            }

            // returns item box.
            return(itemBox);
        }
    }