- Home /
Remove and Destroy Object from Linked List C#
I am trying to remove and destroy transform objects from the front of a linked list. When I get the object that I want to destroy and try and destroy it, I get this error : Cannot implicitly convert type System.Collections.Generic.LinkedListNode' to 'UnityEngine.Transform
I have tried casting it to Transform, but I get this error: Cannot convert type System.Collections.Generic.LinkedListNode' to `UnityEngine.Transform'
Any help would be greatly appreciated.
private static LinkedList<Transform> PlatLR = new LinkedList<Transform> ();
if (PlatLR.Count != 0) {
Transform P = PlatLR.First;
PlatLR.RemoveFirst ();
Destroy(P);
}
Answer by Bunny83 · Jul 10, 2016 at 02:59 PM
The First property is of type LinkedListNode and not the transform itself. You have to use
Transform p = PlatLR.First.Value;
Also like Malleck already said you can't destroy a Transform component, only the host gameobject.
So you have to do:
Destroy(p.gameObject);
Thanks Bunny, I wasn't 100% sure myself but after reading the documentation I figured that. Cheers for confir$$anonymous$$g!
Answer by Malleck666 · Jul 10, 2016 at 12:41 PM
How about...
Destroy(P.GameObject);
If I recall rightly, you can destroy a GameObject but not via the transform, but do correct me if I am wrong.