- Home /
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[i], 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?.
Answer by mangosauce_ · Mar 20, 2021 at 03:14 PM
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[i];
// 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! :)
Hi @mangosauce_ Thanks for the script corrections with the detailed explanations, I appreciate it. The instantiating now it's working pretty well.
but when I call the function to remove objects, it remove all instances and items from the list at once.
maybe its because im calling the function RemoveObjects() from the wrong gameobject? Im calling it from the "$$anonymous$$" button added in the clone instance (as you can see in the screenshot).
PD: I uncomment the Fill(), in the void start because I add the gameobjects to the list in runtime, in the start need to be empty.
I see! In that case it's a little more complicated. You need some way of identifying which GameObject you want to destroy when you press the button. Then, using this new RemoveObject method, pass that GameObject as an argument. The method will check if your list contains the object to destroy, then remove/destroy it.
public void RemoveObject(GameObject o)
{
// Check if the list contains the gameObject
// we want to destroy
if (Objects3dCodigo.Contains(o))
{
// Remove the object from the list
Objects3d.Codigo.Remove(o);
// Destroy the object
Destroy(o);
}
}
If you're assigning GameObjects to their respective Delete buttons, it should be easy to add a listener to the OnClick event of that button. I found a super quick video that might give you an idea on how to do this.
Thanks, Its working now with the new method. I need to make some changes to adequate it to my needs, but the method works. thanks for your help and time.