Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ShantiB95 · Oct 23, 2018 at 01:31 PM · prefabtextscrollviewinstantiate prefabdestroygameobject

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?

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Vicarian · Oct 23, 2018 at 03:14 PM 0
Share

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];
 // ...

avatar image ShantiB95 Vicarian · Oct 23, 2018 at 11:57 PM 0
Share

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?

avatar image ShantiB95 · Oct 24, 2018 at 01:03 AM 0
Share

Now I used DestroyImmediate and the results are listed correctly but apart from the required child objects, some additional objects are being instantiated too.

@$$anonymous$$iwasi @Ryiah @jvo3dc

avatar image ShantiB95 ShantiB95 · Oct 24, 2018 at 01:42 AM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

@Vicarian

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

122 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

prefabs aren't letting me connect a text public thing to it 1 Answer

How to keep score count for each prefab. 1 Answer

GUIText won't update from prefab 1 Answer

Instantiate is spawning too many clones 2 Answers

How to instantiate particle effect after destroying it 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges