Address
304 North Cardinal St.
Dorchester Center, MA 02124
Work Hours
Monday to Friday: 7AM - 7PM
Weekend: 10AM - 5PM
We use cookies to ensure that we provide you with the best possible experience on our site.
Your essential source on Unity and web development
Your essential source on Unity and web development
On Unity, there are several functions used to remove an item from a list.
These methods also work for any type of C# project.
To remove an element from a Unity list, the Remove
method is used, passing the element to be removed as an argument.
When the element is present and removed from the list, the function returns true
.
using System.Collections.Generic;
UnityEngine;
public class TrackerMonoBehaviour : MonoBehaviour
{
public List<GameObject> trackGameObjects = new List<GameObject>();
public void StopSelfTracking()
{
if (trackGameObjects.Remove(gameObject))
{
// Success Remove Tracking
}
}
}
Note: The element passed as an argument must be of the same type as the list or a child type.
For further information on lists, please consult : Unity List Collection: A Comprehensive Guide
It is important to note that lists do not function like dictionaries.
If an element is removed, the indexes are adjusted.
For example, removing the element at index 0 modifies the following indexes: (1 becomes 0, 2 becomes 1, etc.).
It is not advisable to remove elements while looping through the list, as it could lead to errors or inconsistencies.
It might be wiser to clone the list beforehand or use RemoveAll.
An element can also be removed by its index using the RemoveAt
method, passing the index as an argument.
Reminder: Index in lists starts at 0.
var ints = new List<int>() { 5, 4, 2 };
// remove first element
ints.RemoveAt(0);
Note: It is advisable, when removing an element by index, to check beforehand if the index is present in the list using Count to avoid “index out of range” errors.
A very useful tip when you want to remove the last element from a list or an array:
Start by getting the index of the last element using Count while subtracting 1: int lastIndex = myList.Count - 1;
Then use RemoveAt: myList.RemoveAt(lastIndex);
var ints = new List<int>() { 5, 4, 2 };
ints.RemoveAt(ints.Count - 1);
To directly remove multiple elements, RemoveRange can be used.
It is used when you want to remove several consecutive elements.
This function takes 2 parameters, the first the index designating where we want to start deleting, the second to indicate the number of elements we’re going to delete.
private void RemoveRange()
{
var ints = new List<int>() { 5, 4, 2 };
// remove 2 values starting at index 1
ints.RemoveRange(1, 2);
// only the first value remains
// List<int>() { 5 };
}
To remove multiple elements at once, RemoveAll is used.
This function takes a lambda expression as a parameter.
This is used to create a condition; if an item in the list matches this condition, it will be deleted.
public List<GameObject> gameObjects = new List<GameObject>();
private void RemoveWithTagPlayer()
{
// remove all gameObject with the player
gameObjects.RemoveAll(x => x.CompareTag("Player"));
}
In Unity, there are different methods to delete elements from a list.
Each of them is to be used in a specific situation.
I caution against using Remove in a loop, as it may lead to unintended situations.
Thank you for reading the article. Have fun on Unity!
Base image from OpenIcons.