Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question TMP text that was changed from script is overridden by the one in editor

Discussion in 'UGUI & TextMesh Pro' started by keremuslular, May 10, 2022.

  1. keremuslular

    keremuslular

    Joined:
    Feb 10, 2021
    Posts:
    1
    Hello there! I have a prefab object that has a tmp object in it. I add the prefab to a scrollview 3 times from an empty gameObject(manager)'s start function and then call a function in the prefab's script to change the text inside.

    LevelsManager.cs
    Code (CSharp):
    1. public class LevelsManager : MonoBehaviour {
    2.         private LevelSection[] sections;
    3.         private GameObject content;
    4.        
    5.         public GameObject levelSectionTabPrefab;
    6.  
    7.         private void Awake() {
    8.             content = GameObject.Find("Content");
    9.         }
    10.  
    11.         private void Start() {
    12.             var jsonSettings = new JsonSerializerSettings {
    13.                 NullValueHandling = NullValueHandling.Ignore,
    14.                 DefaultValueHandling = DefaultValueHandling.Ignore
    15.             };
    16.             sections = JsonConvert.DeserializeObject<LevelSection[]>(File.ReadAllText(Application.streamingAssetsPath + "/LevelData/dawn_section.json"), jsonSettings);
    17.  
    18.             foreach (var levelSection in sections) {
    19.                 var sectionTab = Instantiate(levelSectionTabPrefab, content.transform);
    20.                 sectionTab.GetComponent<LevelSectionTab>().BuildModel(levelSection);
    21.                 sectionTab.transform.SetSiblingIndex(0);
    22.             }
    23.         }
    24.     }
    LevelSectionTab.cs
    Code (CSharp):
    1. public class LevelSectionTab : MonoBehaviour {
    2.  
    3.         private LevelSection section;
    4.  
    5.         private TextMeshProUGUI title;
    6.         private TextMeshProUGUI count;
    7.         private GameObject completed;
    8.  
    9.         private void Awake() {
    10.             expandButton = GameObject.Find("ExpandButton");
    11.             title = GameObject.Find("Title").GetComponent<TextMeshProUGUI>();
    12.             count = GameObject.Find("Count").GetComponent<TextMeshProUGUI>();
    13.             completed = GameObject.Find("Completed");
    14.         }
    15.        
    16.         public void BuildModel(LevelSection section) {
    17.             this.section = section;
    18.             title.text = section.name.ToUpper();
    19.             count.text = section.firstLevel + " - " + section.lastLevel;
    20.             completed.SetActive(section.completed);
    21.         }
    22.     }
    When I do this, the prefabs are not laid out as it's supposed to. The first two prefabs' tmp texts are all blank and only the last one is laid out correctly. I think it is a lifecycle issue but I'm not certain.

    All comments are helpful. Thank you!!
     
  2. pixaware_pwedrowski

    pixaware_pwedrowski

    Joined:
    Oct 25, 2018
    Posts:
    116
    First of all, do not use GameObject.Find. It's better to cache components in the editor - you can use [SerializeField] to cache private fields in the editor.

    Second of all GameObject.Find returns the FIRST matching object in a scene, so in all Awake methods of LevelSectionTabs, you are caching the same object.

    Cache title, count, and completed fields in an editor - it should solve your issue.
     
    keremuslular likes this.