- Home /
Removing a Component while in a List
Hi, I am wondering what the result of having
List<MyComponent> Components = new List<MyComponent>();
would be after removing a GameObject with its MyComponent in that list. The reason I am wondering this is because the way Unity handles components seems odd to me. Removing a component while it's stored in a variable causes that variable to equal false.
Would the List no longer contain that entry, or would it just be the same as a variable (and equal false)?
Thanks.
Answer by pako · Jan 11, 2015 at 09:59 PM
If the Components List holds a reference in one of its elements to an instance of MyComponent that is attached to a GameObject instance, then that list element will become null, because the instance of MyComponent is destroyed together with the destroyed GameObject. In other words, that particular list element that used to reference a specific instance of MyComponent, references nothing any more, because what it was referencing has been destroyed.
The List element would be the same as a reference variable and equal null (not false).