First of all i must say i have spent all my day searching my answer before i posted this question here. The closest answer i get is here but some of the answers didn't work for me while others are too complicated for me to understand. Let me explain clearly what i want to achieve: For simplicity lets think i have a list that consists of "6" objects. I randomly select an index in the list, lets say, myList[index=2]. Then i want this object to go at the end of the list meaning its index value is now 5. Also i want to re-arrange the untouched objects to get the same size of list without empty index value. After all these steps it should like this:
At the moment my code is this:
public static void Move<T>(this List<T> list, int oldIndex, int newIndex)
{
if ((oldIndex == newIndex) || (0 > oldIndex) || (oldIndex >= list.Count) || (0 > newIndex) ||
(newIndex >= list.Count)) return;
// local variables
var i = 0;
T tmp = list[oldIndex];
for (i = oldIndex+1; i < newIndex; i++)
{
list[i] = list[i - 1];
}
list[newIndex] = tmp;
}
However, as you can predict it doesn't work. Debug.DrawLine to gameobjects in this list shows me when i move the items through the list with my code leave me two objects one myList[0] and one myList[5] which i added. The other lines disappears after.