Store Instantiated Object in LinkedList

Hi, I want to store Instantiated Objects in a linkedList and perform some operations to them. For example, move or destroy the objects that in the linkedlist.
The problem is although i can store them in linkedlist, when i perform operation such as translate or delete, it doesn’t affect them at all.
anybody can help ?

this is example of my code :

public static LinkedList<GameObject> itmList1 = new LinkedList<GameObject>();
...
GameObject tmpItm1 = Instantiate(item, new Vector3(tmpItm.position.x, tmpItm.position.y, tmpItm.position.z), Quaternion.identity) as GameObject ;
itmList1.AddLast(tmpItm1);

...

// accessing the linkedlist 
foreach (GameObject obj in itmList1)
{
    obj.transform.Translate(100,100,100);           
}

Make sure that item is also of type GameObject. If you clone the Transform (which will of course clone the containing GameObject as well) Instantiate will return a reference to the transform and that can’t be casted into GameObject. Since you cast with “as” the retruned reference would be null in this case and you should get null-ref-exceptions in your foreach.

Beside that i can’t see any problem with this code (however i never used the LinkedList, only List,Dictionary,… but i guess it should work that way). Maybe your instantiated object have a script that moves the object? Or an animation that affects the root bone?

I’m sure (100,100,100) is just a test but watch out, Translate moves the object relative and if no other Space is given in local space.

Ok Thanks for the answer.

as you said, the returned reference is null.
so I change the GameObject type to Transform. It work well.
yes, (100,100,100) is just a test :slight_smile:
This is the code :

public static LinkedList itmList1 = new LinkedList();

Transform tmpItm1 = Instantiate(item, new Vector3(tmpItm.position.x, tmpItm.position.y, tmpItm.position.z), Quaternion.identity) ;
itmList1.AddLast(tmpItm1);

// accessing the linkedlist
foreach (Transform obj in itmList1)
{
obj.transform.Translate(100,100,100);