- Home /
How can I change the index of an array object?
I am using C# and an ArrayList containing GameObjects. It's basically a queue system where objects get added to the array, "object 0" gets processed and then removed from the list and we repeat until the list is empty. What I want to do is basically 'shift' an object in the area from index 0 to index 1 and then continue processing on index 0 as usual. This will basically say "don't process me right now, do the next item first and ask me again".
How do I shift the object at ArrayList[0] to ArrayList[1]?
Answer by robertbu · Mar 13, 2014 at 03:41 AM
It is unclear what you are asking here. It sounds like you just want to swap element 0 for element 1. If so, just swap (using a temporary variable). If you really want to insert a new element, the ArrayList class has an Insert method:
http://msdn.microsoft.com/en-us/library/system.collections.arraylist.insert(v=vs.110).aspx
Answer by Kilroymyboy · Mar 13, 2014 at 05:32 AM
The ADT (abstract data type) your referencing is called a Queue, they have queues inside unity
This will add objects in the front, and remove from the front.
its called FIFO (first in first out)
heres an example that may help you
Queue myQ = new Queue();
myQ.Enqueue("Hello");
myQ.Enqueue("World");
myQ.Enqueue("!");
//This would add them inside like this
//0:Hello 1:World 2:!
//Dequeue will remove 0 first, and shift them over for you.
String temp = myQ.Dequeue();
// string now would contain "Hello"
//and the Queue would now contain
0:World 1:!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Issue accessing a GameObject through an Arraylist 2 Answers
Calling Game object from Element list? 0 Answers