Problems with Unity Queues

I have been haveing a problem with the built-in unity queue system where unity will not recognize any of the commands for a queue ( myQ.dequeue(); , myQ.Enqueue, etc).i have two scripts where one is attched to a blue and red box that on click get added to the queue and when fire 3 (left shift) is pressed is supposed to spawn in front of the player.i dont know if im doing something wrong here i was using this website as reference.

Queue Script

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

public class Queue : MonoBehaviour {

    //blue cube to spawn
    public GameObject BlueCube;

    //red cube to spawn
    public GameObject RedCube;

    //postion to spawn the cubes
    public Transform NewPostion;

    //Queue Creation
    public Queue myQ = new Queue();


    // Use this for initialization
    void Start () {
     
    }
   
    // Update is called once per frame
    void Update () {

       
        //if left shift is pressed
        if (Input.GetKeyDown("Fire 3"))
        {
            //if the first item in the queue is the string "Blue Cube"
            //start a 1 second timer then remove it from the queue and render it at the cube spawn point
            if (myQ.first == "Blue Cube")
            {
                StartCoroutine(BlueCubeBuild());
                myQ.dequeue();
                Instantiate(BlueCube, NewPostion.position, Quaternion.identity);
            }

            //if the first item in the queue is the string "Red Cube"
            //start a 2 second timer then remove it from the queue and render it at the cube spawn point
            if (myQ.first == "Red Cube")
            {
                StartCoroutine(RedCubeBuild());
                myQ.dequeue();
                Instantiate(RedCube, NewPostion.position, Quaternion.identity);

            }
        }
    }


    //timer for the blue cube
    IEnumerator BlueCubeBuild()
    {
        yield return new WaitForSeconds(1);
    }
    // timer for the red cube
    IEnumerator RedCubeBuild()
    {
        yield return new WaitForSeconds(2);
    }


   
}

MouseDown Script

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

public class MouseDown : MonoBehaviour {

    //adding the script Queue to this script so it recognizes myQ and its command
    public Queue myQ;

    //when mouse button is pressed on the cube
    void OnMouseDown()
    {

        //if the tage equals "Blue Cube" write to console "Blue Cube Clicked"
        // then add "Blue Cube" to queue
        if (gameObject.tag == "Blue Cube")
        {
            Debug.Log("Blue Cube Clicked");
            myQ.Enqueue("Blue Cube");

        }
        //if the tage equals "Red Cube" write to console "Red Cube Clicked"
        // then add "Red Cube" to queue
        else if (gameObject.tag == "Red Cube")
        {
            Debug.Log("Red Cube Clicked");
            myQ.Enqueue("Red Cube");
        }
    }


    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
       
    }
}

What’s the error you’re getting if any?

Is it something about ‘dequeue’ not existing as a member on Queue?

This is probably because it’s spelled ‘Dequeue’ not ‘dequeue’. C# is case-sensitive.

Furthermore your ‘Queue’ script never queues anything. It only ever calls dequeue. If you don’t add anything, nothing can be dequeue’d.

And your ‘MosueDown’ script only ever Enqueues things. Never Dequeue’ing them.

What is your intent here?

A couple other things. Your Coroutines are not doing anything. Also you have a monobehaviour called queue but then you are creating a queue. Are you sure you are instantiating the correct one? You may need to add system.collection.queue to create the one you referenced.

1 Like

the error i seem to be getting is ‘Queue’ does not contain a definition for ‘Enqueue’ and no extension method ‘Enqueue’ accepting a first argument of type ‘Queue’ could be found, i also tried to spell dequeue with a capital D and it did not change the error.my intent is to have the mouse down script add the strings “blue cube” and “red cube” to the queue then have both scripts attached to each other then the queue script attached the player object (Queue script)will take the strings in the queue and when left shift is pressed have the cube in the first spot of the queue spawn in front of the player.thank you for your help.

could you elaborate on what you mean by my couroutines not doing anything by my knowledge they have a wait time attached to them, i may be mistaken though also when i added using system.collection.queue it gave me the error
A ‘using namespace’ directive can only be applied to namespaces; ‘Queue’ is a type not a namespace. Consider a ‘using static’ directive instead

He means this coroutine

    // timer for the red cube
    IEnumerator RedCubeBuild()
    {
        yield return new WaitForSeconds(2);
    }

does nothing useful. Coroutines don’t work the way you think they work. Let’s take a look at the calling code:

            if (myQ.first == "Red Cube")
            {
                StartCoroutine(RedCubeBuild());
                myQ.dequeue();
                Instantiate(RedCube, NewPostion.position, Quaternion.identity);
            }

Here, StartCoroutine(RedCubeBuild()) is just a couple of function calls. There’s nothing stopping myQ.dequeue() and Instantiate from executing immediately afterwards.

In other words, you’ve created a coroutine object, dequeue’d, and instantiated a cube. Two seconds later, the coroutine finishes.

If you meant to instantiate a new cube after waiting 2 seconds, then it needs to be inside the coroutine, after the yield return statement, like so:

IEnumerator BuildCube(float delay, GameObject cube, Vector3 position)
{
  yield return new WaitForSeconds(delay);
  myQ.dequeue();
  Instantiate(cube, position, Quaternion.identity);
}

And you start it like so: StartCoroutine(BuildCube(2.0f, RedCube, NewPosition.position))

okay thank you for your help that makes a lot more sense