Skip to content

ScriptableList<T> Class

A UnityEngine.ScriptableObject that holds a List<T>.

C#
public abstract class ScriptableList<T> : ScriptableList, INotifyPropertyChanging, INotifyPropertyChanged, IStackTraceProvider, ICanBeReadOnly, INotifyCollectionChanged, IList<T>, ICollection<T>, IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable, INotifyScriptableCollectionChanged<T>

T
The type of elements in the list.

Inheritance object → UnityEngine.Object → UnityEngine.ScriptableObject → RuntimeScriptableObjectScriptableList

Implements INotifyPropertyChanging, INotifyPropertyChanged, IStackTraceProvider, ICanBeReadOnly, INotifyCollectionChanged, IList<T>, ICollection<T>, IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable, INotifyScriptableCollectionChanged<T>

Gets the total number of elements the internal data structure can hold without resizing.

C#
public override sealed int Capacity { get; set; }

Gets the number of elements contained in the ScriptableList<T>.

C#
public override sealed int Count { get; protected set; }

Gets or sets the element at the specified index.

C#
public T this[int index] { get; set; }

If read only, the ScriptableList<T> cannot be changed at runtime and won’t be cleared on start.

C#
public override bool IsReadOnly { get; set; }

If true, an equality check will be run before setting an item through the indexer to make sure the new object is not the same as the old one.

C#
public override bool SetEqualityCheck { get; set; }

If true, the ScriptableList<T> will be cleared on play mode start/game boot.

C#
public override bool ClearOnStart { get; set; }

Adds an item to the end of the ScriptableList<T>.

C#
public void Add(T item)

item T
The object to be added to the end of the . The value can be null for reference types.

ReadOnlyException
If the object is read-only and the application is playing.

Adds the elements of the specified collection to the end of the ScriptableList<T>.

C#
public void AddRange(IEnumerable<T> collection)

collection IEnumerable<T>
The collection whose elements should be added to the end of the . The collection itself cannot be null, but it can contain elements that are null, if type T is a reference type.

ArgumentNullException
collection is null.

ReadOnlyException
If the object is read-only and the application is playing.

Searches the entire sorted ScriptableList<T> for an element using the default comparer and returns the zero-based index of the element.

C#
public int BinarySearch(T item, IComparer<T>? comparer = null)

item T
The object to locate. The value can be null for reference types.

comparer IComparer<T>
The implementation to use when comparing elements, or null to use the default comparer .

int
The zero-based index of item in the sorted ScriptableList<T>, if item is found; otherwise, a negative number that is the bitwise complement of the index of the next element that is larger than item or, if there is no larger element, the bitwise complement of Count.

InvalidOperationException
comparer is null, and the default comparer cannot find an implementation of the IComparer<T> generic interface or the IComparable interface for type T.

Searches a range of elements in the sorted ScriptableList<T> for an element using the specified comparer and returns the zero-based index of the element.

C#
public int BinarySearch(int index, int count, T item, IComparer<T>? comparer = null)

index int
The zero-based starting index of the range to search.

count int
The length of the range to search.

item T
The object to locate. The value can be null for reference types.

comparer IComparer<T>
The implementation to use when comparing elements, or null to use the default comparer .

int
The zero-based index of item in the sorted ScriptableList<T>, if item is found; otherwise, a negative number that is the bitwise complement of the index of the next element that is larger than item or, if there is no larger element, the bitwise complement of index plus count.

ArgumentOutOfRangeException
index is less than 0. -or- count is less than 0.

ArgumentException
index and count do not denote a valid range in the ScriptableList<T>.

InvalidOperationException
comparer is null, and the default comparer cannot find an implementation of the IComparer<T> generic interface or the IComparable interface for type T.

Removes all elements from the ScriptableList<T>.

C#
public override sealed void Clear()

ReadOnlyException
If the object is read-only and the application is playing.

Determines whether an element is in the ScriptableList<T>.

C#
public bool Contains(T item)

item T
The object to locate in the . The value can be null for reference types.

bool
true if item is found in the ScriptableList<T>; otherwise, false.

Converts the elements in the current ScriptableList<T> to another type, and returns a list containing the converted elements.

C#
public List<TOutput> ConvertAll<TOutput>(Converter<T, TOutput> converter)

converter Converter<T, TOutput>
A delegate that converts each element from one type to another type.

List<TOutput>
A List<T> of the target type containing the converted elements from the current ScriptableList<T>.

ArgumentNullException
converter is null.

ConvertAll(ICollection, Converter<T, TOutput>)

Section titled “ConvertAll(ICollection, Converter<T, TOutput>)”

Converts the elements in the current list to another type, and copies the converted elements to the specified list.

C#
public void ConvertAll<TOutput>(ICollection<TOutput> destinationList, Converter<T, TOutput> converter)

destinationList ICollection<TOutput>
The destination list where the converted items will be copied to.

converter Converter<T, TOutput>
A delegate that converts each element from one type to another type.

ArgumentNullException
converter is null.

ReadOnlyException
destinationList is marked as read-only.

Copies the entire ScriptableList<T> to a compatible one-dimensional array, starting at the beginning of the target array.

C#
public void CopyTo(T[] array)

array T[]
The one-dimensional array that is the destination of the elements copied from . The array must have zero-based indexing.

ArgumentNullException
array is null.

ArgumentException
The number of elements in the source ScriptableList<T> is greater than the number of elements that the destination array can contain.

Copies the entire list to a compatible one-dimensional array, starting at the specified index of the target array.

C#
public void CopyTo(T[] array, int arrayIndex)

array T[]
The array destination.

arrayIndex int
The index in the destination array where the copying begins.

Ensures that the list has at least the specified capacity.

C#
public override sealed void EnsureCapacity(int capacity)

capacity int
The minimum capacity to ensure.

Determines whether the ScriptableList<T> contains elements that match the conditions defined by the specified predicate.

C#
public bool Exists(Predicate<T> match)

match Predicate<T>
The delegate that defines the conditions of the elements to search for.

bool
true if the ScriptableList<T> contains one or more elements that match the conditions defined by the specified predicate; otherwise, false.

ArgumentNullException
match is null.

Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire ScriptableList<T>.

C#
public T? Find(Predicate<T> match)

match Predicate<T>
The delegate that defines the conditions of the element to search for.

T
The first element that matches the conditions defined by the specified predicate, if found; otherwise, the default value for type T.

ArgumentNullException
match is null.

Retrieves all the elements that match the conditions defined by the specified predicate.

C#
public List<T> FindAll(Predicate<T> match)

match Predicate<T>
The delegate that defines the conditions of the elements to search for.

List<T>
A List<T> containing all the elements that match the conditions defined by the specified predicate, if found; otherwise, an empty List<T>

ArgumentNullException
match is null.

Retrieves all the elements that match the conditions defined by the specified predicate and copies them to the specified list.

C#
public void FindAll(IList<T> destinationList, Predicate<T> match)

destinationList IList<T>
The list where the found elements will be copied to.

match Predicate<T>
The delegate that defines the conditions of the elements to search for.

ArgumentNullException
match is null.

ArgumentException
destinationList is marked as read-only.

Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the entire list.

C#
public int FindIndex(Predicate<T> match)

match Predicate<T>
The delegate that defines the conditions of the element to search for.

int
The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.

ArgumentNullException
match is null.

Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the range of elements in the list that extends from the specified index to the last element.

C#
public int FindIndex(int startIndex, Predicate<T> match)

startIndex int
The zero-based starting index of the search.

match Predicate<T>
The delegate that defines the conditions of the element to search for.

int
The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.

ArgumentNullException
match is null.

ArgumentOutOfRangeException
startIndex is outside the range of valid indexes for the list.

Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the range of elements in the list that starts at the specified index and contains the specified number of elements.

C#
public int FindIndex(int startIndex, int count, Predicate<T> match)

startIndex int
The zero-based starting index of the search.

count int
The number of elements in the section to search.

match Predicate<T>
The delegate that defines the conditions of the element to search for.

int
The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.

ArgumentNullException
match is null.

ArgumentOutOfRangeException
startIndex is outside the range of valid indexes for the list. Or count is less than 0. Or startIndex and count do not specify a valid section in the list.

Searches for an element that matches the conditions defined by the specified predicate, and returns the last occurrence within the entire list.

C#
public T? FindLast(Predicate<T> match)

match Predicate<T>
The delegate that defines the conditions of the element to search for.

T
The last element that matches the conditions defined by the specified predicate, if found; otherwise, the default value for type T.

ArgumentNullException
match is null.

Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the last occurrence within the entire list.

C#
public int FindLastIndex(Predicate<T> match)

match Predicate<T>
The delegate that defines the conditions of the element to search for.

int
The zero-based index of the last occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.

ArgumentNullException
match is null.

Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the last occurrence within the range of elements in the list that extends from the first element to the specified index.

C#
public int FindLastIndex(int startIndex, Predicate<T> match)

startIndex int
The zero-based starting index of the backward search.

match Predicate<T>
The delegate that defines the conditions of the element to search for.

int
The zero-based index of the last occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.

ArgumentNullException
match is null.

ArgumentOutOfRangeException
startIndex is outside the range of valid indexes for the list.

Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the last occurrence within the range of elements in the list that contains the specified number of elements and ends at the specified index.

C#
public int FindLastIndex(int startIndex, int count, Predicate<T> match)

startIndex int
The zero-based starting index of the backward search.

count int
The number of elements in the section to search.

match Predicate<T>
The delegate that defines the conditions of the element to search for.

int
The zero-based index of the last occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.

ArgumentNullException
match is null.

ArgumentOutOfRangeException
startIndex is outside the range of valid indexes for the list. Or count is less than 0. Or startIndex and count do not specify a valid section in the list.

Performs the specified action on each element of the list.

C#
public void ForEach(Action<T> action)

action Action<T>
The delegate to perform on each element of the list.

ArgumentNullException
action is null.

InvalidOperationException
An element in the collection has been modified.

Returns an enumerator that iterates through the list.

C#
public List<T>.Enumerator GetEnumerator()

List<T>.Enumerator
A List<T>.Enumerator for the list.

Creates a shallow copy of a range of elements in the source list.

C#
public List<T> GetRange(int index, int count)

index int
The zero-based list index at which the range starts.

count int
The number of elements in the range.

List<T>
A shallow copy of a range of elements in the source list.

ArgumentOutOfRangeException
index is less than 0. Or count is less than 0.

ArgumentException
index and count do not denote a valid range of elements in the list.

Copies a range of elements from the source list to the specified destination list.

C#
public void GetRange(int index, int count, IList<T> destinationList)

index int
The zero-based list index at which the range starts.

count int
The number of elements in the range.

destinationList IList<T>
The list where the copied elements will be stored.

ArgumentNullException
destinationList is null.

ArgumentOutOfRangeException
index is less than 0. Or count is less than 0.

ArgumentException
index and count do not denote a valid range of elements in the list.

ArgumentException
destinationList is marked as read-only.

Searches for the specified object and returns the zero-based index of the first occurrence within the entire list. Returns -1 if the item is not found.

C#
public int IndexOf(T item)

item T
The object to locate in the list.

int
The zero-based index of the first occurrence of item within the entire list, if found; otherwise, -1.

Searches for the specified object and returns the zero-based index of the first occurrence within the range of elements in the list that extends from the specified index to the last element.

C#
public int IndexOf(T item, int index)

item T
The object to locate in the list.

index int
The zero-based starting index of the search. 0 (zero) is valid in an empty list.

int
The zero-based index of the first occurrence of item within the range of elements in the list that extends from index to the last element, if found; otherwise, -1.

ArgumentOutOfRangeException
index is outside the range of valid indexes for the list.

Searches for the specified object and returns the zero-based index of the first occurrence within the range of elements in the list that starts at the specified index and contains the specified number of elements.

C#
public int IndexOf(T item, int index, int count)

item T
The object to locate in the list.

index int
The zero-based starting index of the search. 0 (zero) is valid in an empty list.

count int
The number of elements in the section to search.

int
The zero-based index of the first occurrence of item within the range of elements in the list that extends from index to the last element, if found; otherwise, -1.

ArgumentOutOfRangeException
index is outside the range of valid indexes for the list. Or count is less than 0. Or index and count do not specify a valid section in the list.

Inserts an element into the list at the specified index.

C#
public void Insert(int index, T item)

index int
The index where the item should be inserted.

item T
The object to insert.

ReadOnlyException
If the object is read-only and the application is playing.

Inserts the elements of a collection into the list at the specified index.

C#
public void InsertRange(int index, IEnumerable<T> collection)

index int
The zero-based index at which the new elements should be inserted.

collection IEnumerable<T>
The collection whose elements should be inserted into the list.

ArgumentNullException
If collection is null.

ArgumentOutOfRangeException
If index is less than 0 or is greater than Count.

ReadOnlyException
If the object is read-only and the application is playing.

Searches for the specified object and returns the zero-based index of the last occurrence within the entire list.

C#
public int LastIndexOf(T item)

item T
The object to locate in the list.

int
The zero-based index of the last occurrence of item within the entire the list, if found; otherwise, -1.

Searches for the specified object and returns the zero-based index of the last occurrence within the range of elements in the list that extends from the first element to the specified index.

C#
public int LastIndexOf(T item, int index)

item T
The object to locate in the list.

index int
The zero-based starting index of the backward search.

int
The zero-based index of the last occurrence of item within the range of elements in the list that extends from the first element to index, if found; otherwise, -1.

ArgumentOutOfRangeException
index is outside the range of valid indexes for the list.

Searches for the specified object and returns the zero-based index of the last occurrence within the range of elements in the list that contains the specified number of elements and ends at the specified index.

C#
public int LastIndexOf(T item, int index, int count)

item T
The object to locate in the list.

index int
The zero-based starting index of the backward search.

count int
The number of elements in the section to search.

int
The zero-based index of the last occurrence of item within the range of elements in the list that contains count number of elements and ends at index, if found; otherwise, -1.

ArgumentOutOfRangeException
index is outside the range of valid indexes for the list. Or count is less than 0. Or index and count do not specify a valid section in the list.

Removes the first occurrence of a specific object from the ScriptableList<T>.

C#
public bool Remove(T item)

item T
The object to remove from the . The value can be null for reference types.

bool
true if item was successfully removed from the ScriptableList<T>; otherwise, false. This method also returns false if item is not found in the original ScriptableList<T>.

ReadOnlyException
If the object is read-only and the application is playing.

Removes all the elements that match the conditions defined by the specified predicate.

C#
public int RemoveAll(Predicate<T> match)

match Predicate<T>
The delegate that defines the conditions of the elements to remove.

int
The number of elements removed from the ScriptableList<T>.

ArgumentNullException
match is null.

ReadOnlyException
If the object is read-only and the application is playing.

Removes the element at the specified index of the ScriptableList<T>.

C#
public void RemoveAt(int index)

index int
The zero-based index of the element to remove.

ArgumentOutOfRangeException
index is less than 0. -or- index is equal to or greater than Count.

ReadOnlyException
If the object is read-only and the application is playing.

Removes a range of elements from the list.

C#
public void RemoveRange(int index, int count)

index int
The zero-based starting index of the range of elements to remove.

count int
The number of elements to remove.

ArgumentOutOfRangeException
If index is less than 0 or greater than Count.

ArgumentOutOfRangeException
If index is less than 0 or greater than Count.

ArgumentOutOfRangeException
If count is less than 0.

ArgumentException
If index and count do not denote a valid range of elements.

ReadOnlyException
If the object is read-only and the application is playing.

Reverses the order of the elements in the entire ScriptableList<T>.

C#
public override sealed void Reverse()

ReadOnlyException
If the object is read-only and the application is playing.

Reverses the order of the elements in the specified range.

C#
public override sealed void Reverse(int index, int count)

index int
The starting index of the range to reverse.

count int
The number of elements in the range to reverse.

ReadOnlyException
If the object is read-only and the application is playing.

Creates a shallow copy of a range of elements in the source list.

C#
public List<T> Slice(int index, int count)

index int
The zero-based list index at which the range starts.

count int
The number of elements in the range.

List<T>
A shallow copy of a range of elements in the source list.

ArgumentOutOfRangeException
index is less than 0. Or count is less than 0.

ArgumentException
index and count do not denote a valid range of elements in the list.

Copies a range of elements from the source list to the specified destination list.

C#
public void Slice(int index, int count, IList<T> destinationList)

index int
The zero-based list index at which the range starts.

count int
The number of elements in the range.

destinationList IList<T>
The list where the copied elements will be stored.

ArgumentNullException
destinationList is null.

ArgumentOutOfRangeException
index is less than 0. Or count is less than 0.

ArgumentException
index and count do not denote a valid range of elements in the list.

ArgumentException
destinationList is marked as read-only.

Sorts the elements in the entire ScriptableList<T> using the specified comparer.

C#
public void Sort(IComparer<T>? comparer = null)

comparer IComparer<T>
The implementation to use when comparing elements, or null to use the default comparer .

InvalidOperationException
comparer is null, and the default comparer cannot find an implementation of the IComparer<T> generic interface or the IComparable interface for type T.

ReadOnlyException
If the object is read-only and the application is playing.

Sorts the elements in a range of elements in list using the specified comparer.

C#
public void Sort(int index, int count, IComparer<T>? comparer = null)

index int
The starting index of the range to sort.

count int
The number of elements in the range to sort.

comparer IComparer<T>
The implementation to use when comparing elements, or null to use the default comparer .

InvalidOperationException
comparer is null, and the default comparer cannot find an implementation of the IComparer<T> generic interface or the IComparable interface for type T.

ReadOnlyException
If the object is read-only and the application is playing.

Sorts the elements in the entire list using the specified Comparison<T>.

C#
public void Sort(Comparison<T> comparison)

comparison Comparison<T>
The to use when comparing elements.

ArgumentNullException
comparison is null.

ReadOnlyException
If the object is read-only and the application is playing.

Copies the elements of the ScriptableList<T> to a new array.

C#
public T[] ToArray()

T[]
An array containing copies of the elements of the ScriptableList<T>.

Sets the capacity to the actual number of elements in the ScriptableList<T>, if that number is less than a threshold value.

C#
public override sealed void TrimExcess()

Determines whether every element in the ScriptableList<T> matches the conditions defined by the specified predicate.

C#
public bool TrueForAll(Predicate<T> match)

match Predicate<T>
The delegate that defines the conditions to check against the elements.

bool
true if every element in the ScriptableList<T> matches the conditions defined by the specified predicate; otherwise, false. If the list has no elements, the return value is true.

ArgumentNullException
match is null.

Tries to find an element in the list that matches the specified predicate.

C#
public bool TryFind(Predicate<T> match, out T? result)

match Predicate<T>
The delegate that defines the condition to check against.

result T
The first result that matches the delegate. If no element was found, this will be the default value.

bool
true if an element was found; otherwise, false.

Called when the game starts, before OnStart().

You should not subscribe to events in this method! Use OnStart() instead. OnPreStart() should be used for preparing the scriptable object.

C#
protected override void OnPreStart()

If this isn’t called when the game starts, it’s called once this scriptable object is loaded and instantiated.

Warns if there are any left-over subscribers to the events.

C#
protected override void WarnIfLeftOverSubscribers()

This will only be called in the Unity editor and builds with the DEBUG flag.

Warns if there are any left-over objects in the dictionary.

C#
[Conditional("DEBUG")]
protected void WarnIfLeftOverObjects()

This will only be called in the Unity editor and builds with the DEBUG flag.

Removes any subscribers from the event.

C#
public void ClearSubscribers(bool warnIfLeftOver = false)

warnIfLeftOver bool
If true, a warning will be printed in the console if there are any subscribers. The warning will only be printed in the editor and debug builds.

Called when the game is ending, after OnPreDisabled().

You should not unsubscribe from events in this method! Use OnPreDisabled() instead. OnDisabled() should be used for cleaning up the scriptable object.

C#
protected override void OnDisabled()

This can also be called during the game is the scriptable object is destroyed and/or unloaded.

Occurs when an item is added, removed, replaced, or the entire ScriptableList<T> is refreshed.

C#
public event CollectionChangedEventHandler<T>? OnCollectionChanged

CollectionChangedEventHandler<T>

Called when something was added. Gives you the newly added item.

C#
[Obsolete("Use 'OnCollectionChanged' instead. This will be removed in build.", true)]
public event Action<T>? OnAdded

Action<T>

Called when something was inserted. Gives you the index it was inserted at and the newly inserted item.

C#
[Obsolete("Use 'OnCollectionChanged' instead. This will be removed in build.", true)]
public event Action<int, T>? OnInserted

Action<int, T>

Called when something was added or inserted. Gives you the index it was added/inserted at and the newly added/inserted item.

C#
[Obsolete("Use 'OnCollectionChanged' instead. This will be removed in build.", true)]
public event Action<int, T>? OnAddedOrInserted

Action<int, T>

Called when something was set using the indexer. Gives you the index it was set at, the old value and the new value.

C#
[Obsolete("Use 'OnCollectionChanged' instead. This will be removed in build.", true)]
public event Action<int, T, T>? OnSet

Action<int, T, T>

Called when something was removed. Gives you the index it was removed at and the removed item.

C#
[Obsolete("Use 'OnCollectionChanged' instead. This will be removed in build.", true)]
public event Action<int, T>? OnRemoved

Action<int, T>

Called when the list is cleared.

C#
[Obsolete("Use 'OnCollectionChanged' instead. This will be removed in build.", true)]
public event Action? OnCleared

Action

Called when the list is changed in any way.

C#
[Obsolete("Use 'OnCollectionChanged' instead. This will be removed in build.", true)]
public event Action<ListChangeType>? OnChanged

Action<ListChangeType>