- Home /
Clear Does not work to remove
I am creating buttons where the buttons will generate prefabs. Then I am creating a list to store each instantiated prefab object, I am now trying to create a button to clear all the instantiated objects from the scene... however it does not seem to be working...
public class objectiveGenerate : MonoBehaviour {
public Button buttonSelect;
public GameObject myPrefab;
public List<GameObject> generatedObjects = new List<GameObject>();
void Start()
{
buttonSelect.onClick.AddListener(objectiveTask);
}
public void objectiveTask()
{
if (EventSystem.current.currentSelectedGameObject.name == "buttonCube")
{
GameObject Cube = Instantiate(myPrefab);
Cube.transform.position = new Vector3(0, 0.5f, 0);
generatedObjects.Add(Cube);
}
else if (EventSystem.current.currentSelectedGameObject.name == "buttonClear")
{
foreach(var obj in generatedObjects)
{
Destroy(obj);
}
generatedObjects.Clear();
}
}
}
Answer by Bonfire-Boy · Jun 11, 2019 at 06:53 PM
You are clearing the list and then going through it destroying its contents.
You probably want to be doing that the other way around.
Pretend like I had that foreach statement commented out. When I run this with only the clear, it doesn't work... then I tried doing it those other ways as well and those do not work either. Is there a reference I am missing or something?
A List of gameobject references is just a list of references. Clearing that list will not affect the objects you had referenced at all. If i write some names onto a sheet of paper, for example:
Donald Trump
George W. Bush
When I clear that sheet of paper using an eraser / rubber or by just destroying that sheet of paper you can be sure the persons I'd referenced on my list aren't affected by clearing my list. If you want to carry out an action for each reference in your list you have to iterate through that list and perform the action.
In your case if you want to destroy all the gameobject you have referenced in your list, you have to do
foreach(var obj in generatedObjects)
{
Destroy(obj);
}
Once you're done with the objects you can clear the list
generatedObjects.Clear();
Of course clearing the list before you've done anything with the objects referenced is pretty pointless. It's like writing a shopping list and before you go shopping you destroy the list. Destroying / clearing the list does not magically buy all the things you had on the list. All you got from clearing the list is that you've lost the information what you wanted to buy.
There are countless of similar analogies in the realworld. Hopefully the point of a list has become more clear.
See that is what I was thinking was happening. I realized the list was being cleared but the actual objects I'm putting into the scene aren't. So I tried the foreach statement as you showed, and I have one of my own. $$anonymous$$y thing is, it still doesn't work. Nothing is happening at all to any of the objects in the screen. So I don't know if I'm just writing to the list incorrectly... is there a way to test that with Debug.log?