Trying to using Array.Copy in the Unity Monodevelop environment, specifically what I'm trying to do is move a value from the first slot of the array into a holder variable, then move every value in the array forward one slot, then move the value from the holder variable back into the Array in the last slot. My relevant code is as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class TurnController : MonoBehaviour {
//Array will hold all units (Being that they all have the EidolonClass script attached), then will be sorted by Speed, descending
private EidolonClass[] AllUnitArray;
...
void Awake(){
//Find anything with the EidolonClass and then add it to the Array
AllUnitArray = FindObjectsOfType (typeof(EidolonClass)) as EidolonClass[];
//Sort the array by speed, descending (Highest speed listed first)
Array.Sort (AllUnitArray, delegate(EidolonClass one, EidolonClass two) {
return two.Speed.CompareTo(one.Speed);
});
}
void pushArray(){
EidolonClass store = AllUnitArray [0];
for(int i=1;i<=AllUnitArray.Length;i++){
Array.Copy (AllUnitArray, i, AllUnitArray, i-1, AllUnitArray.Length-1);
}
AllUnitArray [AllUnitArray.Length] = store;
for(int i=0;i<=AllUnitArray.Length;i++) {
Debug.Log (AllUnitArray[i].name.ToString ());
}
}
void Update () {
if (Input.GetKeyDown (KeyCode.K)) {
pushArray ();
}
}
This code compiles in Monodevelop, but when I try to run this section of my script, it returns the following error:
ArgumentException: length
System.Array.Copy (System.Array sourceArray, Int32 sourceIndex, System.Array destinationArray, Int32 destinationIndex, Int32 length) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Array.cs:971) TurnController.pushArray () (at Assets/Scripts/Battle Scripts/TurnController.cs:54) TurnController.Update () (at Assets/Scripts/Battle Scripts/TurnController.cs:37)