issue with gameobjects and enqueue

here’s some context to what i’m trying to do first, i’m spawning a prefab behind an object as it moves along a position, x times. so the idea is, i’ll instantiate y number of these objects into some sort of container, then access the bottom most one, update, then put it back on top of the stack.

in c++, I’m kind of approaching it in a doubly link list sort of way. over here in c# i’m using the queue.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TrailCollider : MonoBehaviour
{
    private Queue<GameObject> trailQueue;
    public GameObject trailPrefab;
    float startTime;
    public float delay = 30f;
    public int maxsize = 5;
    private bool init = false;

    // Use this for initialization
    void Start ()
    {
        startTime = Time.time;
    }

    void initializeQueue()
    {
        for (int i = 0; i < maxsize; i++)
        {
            trailQueue.Enqueue((GameObject)Instantiate(trailPrefab, transform.position, transform.rotation));
        }

        init = true;
    }
	
	// Update is called once per frame
	void Update ()
    {
        if(!init)
            initializeQueue();

        SpawnNewCircleCollider();
	}

    void SpawnNewCircleCollider()
    {
        float t = Time.time - startTime / delay;

        if (t >= 1)
        {
            GameObject lastelem = trailQueue.Dequeue();
            lastelem.transform.position = transform.position;
            trailQueue.Enqueue(lastelem);
            startTime = Time.time;
        }
    }
}

for some reason, the gameobjects that were instantiated are not being put into the queue. I’m not sure why.

nevermind managed to figure it out. i realised the container also needs to be initialised.

private Queue<GameObject> trailQueue = new Queue<GameObject>();

hope that’s useful to somebody some day :).

Thank you so much, it did help me a lot!

It was useful, thanks!