OK, so it's not documented in the Array class, but if I type in an array name in MonoDevelop, Slice comes up as an option with both one and two parameters. I have a feeling it's not documented because it's horribly broken, at least in the case of the single argument variant, but I wanted to ask to make sure I wasn't misunderstanding how slice is supposed to work in Unity. For example, an array of the integers 0 to 9, arr.Slice(3) = 3,4,5,6 -- single arg should go to end of array, not just pick some arbitrary length arr.Slice(3,6) = 3,4,5 -- exactly what I expect, last index should be left off arr.Slice(3, sliceTest.length) = 3,4,5,6,7,8,9 -- also exactly what I expect arr.Slice(-5) = argument out of range, can't be less than 0 -- negative numbers not allowed, frowny face but I'll live. Anyway, would like any feedback to tell me I'm horribly wrong or I just shouldn't use Slice. Thanks! Code (csharp): var sliceTest : Array = new Array(); for(var i : int = 0; i < 10; i++) { sliceTest.Add(i); } var sliced : Array = sliceTest.Slice(3); Debug.Log("Sliced(3) = " + sliced.ToString()); sliced = sliceTest.Slice(3,6); Debug.Log("Sliced(3,6) = " + sliced.ToString()); sliced = sliceTest.Slice(3,sliceTest.length); Debug.Log("Sliced(3,sliceTest.length) = " + sliced.ToString()); sliced = sliceTest.Slice(3, -2); Debug.Log("Sliced(3, -2) = " + sliced.ToString()); sliced = sliceTest.Slice(-5); Debug.Log("Sliced(-5) = " + sliced.ToString());
there is nothing wrong using slice. Its only your code that makes no sense cause you use -5 as start position for "slice from here on" which logically is bs as an array index must be in interval [0,Array Length - 1].
Well, first off, how do you explain the result of Slice(3) then? Secondly, every other slice I've ever used accepts negative indices to indicate counting backwards from the end of the array. For example, see: http://www.w3schools.com/jsref/jsref_slice_array.asp
Can't explain the result of 3 But on the other end I normally also don't use the slowest available datastructure. If I need dynamic growing etc, I use List<>, if I need removal of specific elements a lot or access specific ranges in the list, then LinkedList<> and if I want to access by Index, I use normal [] arrays ... Basically everything is faster and better than missconcluding what Unitys array class is which is there primarily for "lazyness" I guess (JS / AS users switching over so they don't drop into the dark or whatever)