# Beginner programming question

Hello there! I’ve just started fooling around with unity and have very little experience in coding. I have one question that I cannot find the words to ask so I’ll use an example.

Say I have a BALL script and a BALLMANAGER script. In the ball script there is a Queue of items that must be executed in FIFO format. The ballmanager script instantiates multiple balls as well as their respective ball scripts. How would I have it so that all the instantiated Balls inherit the same Queue and not make a new Queue each time (but after instantiating use the Queue independently)?

You could have the queue stored in the ball manager and have the ball manager share the queue with the balls… then give the balls their own iterators for the queue.

This would cause problems if the balls removed things from and added to the queue though. If the queue is a constant changing thing then this would be a very bad idea. Instead you would need to give each ball an instantiated copy of the queue. So if you are popping the front of the queue when it’s used… you will need to instantiate a copy of the queue.

To instantiate a deep copy of the queue on the ball from manager…

In the instantiation of the ball you could send a message to the ball using

``````GameObject ball = Instiantiate(ballPrefab, position, rotation) as GameObject;
ball.SendMessage("CloneQueue", myQueue);
``````

Where myQueue is the member queue on the ball manager.

In the ball script you will need a member variable for the queue and now add a new function called
CloneQueue with a parameter of the datatype for your Queue, so say if it is a queue of Vector3…

``````Queue<Vector3> myQueue;

void CloneQueue( Queue<Vector3> toClone )
{
myQueue = new Queue<Vector3>(toClone);
}
``````

Because the Queue<> is a reference type to have the balls use them independently you’ll always have to make a new Queue, but to get the functionality you are after you could maybe have a static Queue on your Ball script and an instance Queue and make a deep copy of the static queue when you instantiate your Ball script, for example…

``````public static Queue<Object> mainQueue = new Queue<Object>();

Queue<Object> instanceQueue;

void Start () {
instanceQueue = new Queue<Object>(mainQueue);
}
``````

The mainQueue in this example will be available to all instances so they can grab a copy to use independently.

Hope this helps

Just noticed someone beat me to an answer but i’ll post this anyway as it’s a slightly different method.