How to add & remove specific gameobject from a list and instantiate them

Hello People.

Im trying to add gameobject to a list in runtime and later remove a desired gameobject from it.
at the same time I add gameobjects to the list I need to instantiate them too, to show in a scroll view.
and later destroy the desired instance from a button.

There is my attemp for now:

    public Transform ContentContainer; //used to set where the instantiated objects are added.
    public List<GameObject> Objects3dCodigo = new List<GameObject>();
    public GameObject AddObject;
    private GameObject Clone;
    private void Start()
    {
        //Fill();
    }

    public void Fill()
    {
        Objects3dCodigo.Add(AddObject); //add gameobject to list
        for (int i = 0; i < Objects3dCodigo.Count; i++) //instantiate them
        {
            Clone = Instantiate(Objects3dCodigo*, ContentContainer.transform);* 

}
}

public void RemoveObjects()
{
for (int i = 0; i < Objects3dCodigo.Count; i–)
{
Objects3dCodigo.RemoveAt(i);
Destroy(Clone.gameObject);
}
}
My problems so far:
1- each time I add a new gameobject to the list, the instantiate function multiply the cloned items.
2- when I call RemoveObjects(), only remove the last instance.
Can give me some guidance?.
![alt text][1]
[1]: http://imgfz.com/i/AZkmRt9.png

I made the appropriate fixes to your code and added some comments to help you understand what’s happening. Essentially, you were adding the gameObject prefab (AddObject) to the list, then saving only the most recently instantiated clone of that prefab to Clone. Then, when you were looping through the list in RemoveObject, you were removing the prefab from the list, and destroying the most recently instantiated Clone.


In my revised version of the code, you are instantiating a clone of AddObject, then adding the clone to the list. Then in RemoveObject, you enumerate through the list, removing the clones from the list and destroying them. Hope the code helps explain things a little!


        public Transform ContentContainer;
        public List<GameObject> Objects3dCodigo = new List<GameObject>();
        public GameObject AddObject;

        private void Start()
        {
            // Adds a new gameObject of type
            // AddObject to the Objects3dCodigo list
            Fill();
        }

        public void Fill()
        {
            // Moved Clone variable to be local to the method
            GameObject Clone = Instantiate(AddObject, ContentContainer);
            // We just instantiated a gameObject of type AddObject,
            // then saved its value to Clone. Now we will add the object
            // we just saved to the list of objects.
            Objects3dCodigo.Add(Clone);
        }

        public void RemoveObjects()
        {
            // In order to loop through a list backwards, we
            // need to start at the list's length - 1, then
            // continue ticking down until we reach 0.
            for (int i = Objects3dCodigo.Count - 1; i >= 0; i--)
            {
                // Save the object at position i in the list
                // to the local variable Clone
                GameObject Clone = Objects3dCodigo*;*

// Remove the object from the list
Objects3dCodigo.RemoveAt(i);
// Destroy the object we got from the list
Destroy(Clone);
}
}
----------
Please let me know if this helped any! :slight_smile: