I'm building some somtware that displays a text every time you load a certain screen. I'm trying to delete all previous text so that I can load new text every time I load the view. But for some reason I can't seem to destroy the gameobjects holding the text, and ends up duplicating the text I already had.
private void SetPages(List<string> strings)
{
Clear();
foreach (string page in strings)
AddPage(page);
}
private void AddPage(string text)
{
Text page = instantiator.InstantiatePrefabForComponent<Text>(textPrefab);
page.GetComponent<Text>().text = text;
page.transform.SetParent(root, false);
texts.Add(page);
}
private void Clear()
{
if(texts != null)
{
foreach (Text text in texts)
{
oldTexts.Add(text);
}
for (int i = 0; i < oldTexts.Count; i++)
{
Destroy(oldTexts[i].gameObject);
}
oldTexts.Clear();
texts.Clear();
}
}