Removing objects with children from lists
void Start () {
listScript = GameObject.Find ("ListKeeper").GetComponent<Lists> ();
listScript.EnemyList.Add (gameObject); // Add this unit to unitlist
}
// Update is called once per frame
void OnDestroy () {
listScript.EnemyList.Remove (gameObject); // Remove this unit from unitlist
}
This bit of code adds the enemy game object to a list when it is created. When the enemy dies, I want to remove it from the list. However the enemy object have a child game object, and when I destroy it I get the following error: NullReferenceException: Object reference not set to an instance of an object AddToEnemyList.OnDestroy () (at Assets/AddToEnemyList.cs:16) The same code doesn't give an error with enemies without any children. Everything works, even with the error, but I suppose it looks for the children in the list as well.
Your answer
Follow this Question
Related Questions
Updating a List from a Custom Inspector 0 Answers
Firebase List Users 1 Answer
Add object of specific width to a List 1 Answer
How Do I Create a List of Two Strings? C# 1 Answer
List keeps losing items. 0 Answers