- Home /
Re-instantiated prefabs desist changes in text component
I've created an UI scroll list and after performing certain operations, the list is populated with multiple instances of a prefab containing UI text as one of its components. An operation changes the text within the text component of the prefab. This is performed without any anomalies for the first operation, but subsequent operations fail to change the text as desired. I'm storing the text to be replaced with inside a List of strings. Following is the code:
public List<string> collectionlist = new List<string>{};
/*some code to insert relevant data into the list through a loop*/
//loop for instantiating prefabs into the scroll list (which has the component Vertical Layout
//Group script attached to it).
for(int i=0;i<=collectionlist.Count-1;i++)
{
Instantiate(Prefab,Content.transform,false);
Content.transform.GetChild(i).GetChild(0).GetComponent<Text>().text=collectionlist [i];
}
The list items are to be erased and created repeatedly owing to certain operations. So before proceeding to list the results of of the next operation, I destroy the already instantiated list items by using the following code:
if(Content.transform.childCount>0)
{
foreach (Transform child in Content.transform)
{
Destroy(child.gameObject);
}
}
And proceed to re-instantiate and populate the list with prefabs and to change the text accordingly, but the second or random intermediate operations fail to change the text as desired by the operation and the default prefab text is displayed. I wonder what am I doing wrong?
I'm inclined to think it's a race condition. The text that is updated is likely the Text attached to objects that are getting destroyed. You could perhaps check this by logging the text of the object before assigning it:
// ...
Instantiate(Prefab,Content.transform,false);
Debug.Log(Content.transform.GetChild(i).GetChild(0).GetComponent<Text>().text);
Content.transform.GetChild(i).GetChild(0).GetComponent<Text>().text=collectionlist [i];
// ...
Sorry I missed to add this point! I had already checked the text being updated by logging (when I hadn't put the prefab into an empty game object). For a loop for adding 5 items, the texts in the new child objects updated correctly until the fourth loop, but the finishing loop reset the text to the default prefab text. I checked this by logging the text of only one child for whole of the loop. Now I've enclosed the prefab into an empty game object, and the logs show the correct text for whole of the loop but the text in the game window is incorrect. I guess this has something to do with the destroyed game objects but I'm destroying the old ones altogether before instantiating the new ones. Wonder what's wrong?
I read the docs for Destroy and DestroyImmediate and I believe the anomaly is due to the frame time. It also says, that you should never iterate through arrays and destroy the elements you are iterating over. This will cause serious problems (as a general program$$anonymous$$g practice, not just in Unity). I wonder what could be the alternative for the probable race condition @Vicarian @Wolfram
Answer by ShantiB95 · Jun 14, 2019 at 09:22 PM
This helped here:
using System.Linq;
// [ ... ]
foreach (Transform child in Content.transform.Cast<Transform>().ToArray())
{
Destroy(child.gameobject);
}
Here's the clarification: http://answers.unity.com/comments/1256363/view.html