How to have a Game Object remove itself from a list? (Unity3d)

Asix Jin

OK so basically I have a list that keeps track of all the enemies in the scene. I am using the list primarily so I can have each enemy check other enemies positions so they don't end up on the same spot. When ever an enemy is created it will add itself to a list, but I need the enemy to remove itself once it's killed. I tried what I have in the code below but it throws me an error (ArgumentOutOfRangeException: Argument is out of range).

void Start () 
{
    Manager.EnemyList.Add(this.gameObject);
    ListSpot = Manager.EnemyList.Count; 
}

//Kills the enemy
public void Kill()
{
    Manager.EnemyList.RemoveAt(ListSpot);
    Destroy(this.gameObject);
}
Vengarioth

Use a list from the System.Collections.Generic namespace, then you can just call Add and Remove with the instance of the object. It will use the hash functions of the instance to do its thing.

List<GameObject> enemies = new List<GameObject>();
enemies.Add(enemy1);
enemies.Remove(enemy1);

Edit: your solution throws the exception because the index starts by 0 and count will always begin to count at 1. if you add the first element its position is 0, but the count is 1.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to remove unity3d game engine from Ubuntu (and its dependencies)?

From Dev

Unity3d - How to make method calling from another class that belongs to another game object efficiently

From Dev

how to limit the rotation of a game object in unity3d

From Dev

How to move a game object in the forwrard direction of an other game object Unity3D

From Dev

How to randomly pick value from a list in Unity3D?

From Dev

How can i know touched which game object with Unity3D

From Dev

How to make a object delete itself from a list.

From Dev

How to remove a specific object from a List<T>

From Dev

How to remove padding from a Object List Item?

From Dev

How to remove duplicates from a list of object

From Dev

How to find (and remove) a nested object from a List

From Dev

How to remove a specific object from a List<T>

From Dev

How to remove duplicates from a list of object

From Dev

How to find (and remove) a nested object from a List

From Dev

How to remove an instance of object from a list in python?

From Dev

How to remove an object from a linked list in java

From Dev

Unity3D checking if a point is inside a game object

From Dev

Unity3D async game object instantiation

From Dev

How to design a game in Unity3D which is viable for patching?

From Dev

How to install Unity3d game engine on Ubuntu

From Dev

How to horizontally center a unity3D game in a div?

From Dev

In Unity3D game engine, how to make a delay?

From Dev

How to install Unity3d game engine on Ubuntu

From Dev

How to setup Parental gate for game developed in UNITY3d?

From Dev

How to horizontally center a unity3D game in a div?

From Dev

javafx: How to stop an object's background task running when the object itself is deleted from Observable List?

From Dev

How to remove a field from each object of a list of object?

From Dev

In Python: How to remove an object from a list if it is only referenced in that list?

From Dev

How to remove an object from a list that is not present in another list in R

Related Related

  1. 1

    How to remove unity3d game engine from Ubuntu (and its dependencies)?

  2. 2

    Unity3d - How to make method calling from another class that belongs to another game object efficiently

  3. 3

    how to limit the rotation of a game object in unity3d

  4. 4

    How to move a game object in the forwrard direction of an other game object Unity3D

  5. 5

    How to randomly pick value from a list in Unity3D?

  6. 6

    How can i know touched which game object with Unity3D

  7. 7

    How to make a object delete itself from a list.

  8. 8

    How to remove a specific object from a List<T>

  9. 9

    How to remove padding from a Object List Item?

  10. 10

    How to remove duplicates from a list of object

  11. 11

    How to find (and remove) a nested object from a List

  12. 12

    How to remove a specific object from a List<T>

  13. 13

    How to remove duplicates from a list of object

  14. 14

    How to find (and remove) a nested object from a List

  15. 15

    How to remove an instance of object from a list in python?

  16. 16

    How to remove an object from a linked list in java

  17. 17

    Unity3D checking if a point is inside a game object

  18. 18

    Unity3D async game object instantiation

  19. 19

    How to design a game in Unity3D which is viable for patching?

  20. 20

    How to install Unity3d game engine on Ubuntu

  21. 21

    How to horizontally center a unity3D game in a div?

  22. 22

    In Unity3D game engine, how to make a delay?

  23. 23

    How to install Unity3d game engine on Ubuntu

  24. 24

    How to setup Parental gate for game developed in UNITY3d?

  25. 25

    How to horizontally center a unity3D game in a div?

  26. 26

    javafx: How to stop an object's background task running when the object itself is deleted from Observable List?

  27. 27

    How to remove a field from each object of a list of object?

  28. 28

    In Python: How to remove an object from a list if it is only referenced in that list?

  29. 29

    How to remove an object from a list that is not present in another list in R

HotTag

Archive