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 () {
}
}
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.
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
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: