TMP text that was changed from script is overridden by the one in editor
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
public class LevelsManager : MonoBehaviour {
private LevelSection[] sections;
private GameObject content;
public GameObject levelSectionTabPrefab;
private void Awake() {
content = GameObject.Find("Content");
}
private void Start() {
var jsonSettings = new JsonSerializerSettings {
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore
};
sections = JsonConvert.DeserializeObject<LevelSection[]>(File.ReadAllText(Application.streamingAssetsPath + "/LevelData/dawn_section.json"), jsonSettings);
foreach (var levelSection in sections) {
var sectionTab = Instantiate(levelSectionTabPrefab, content.transform);
sectionTab.GetComponent<LevelSectionTab>().BuildModel(levelSection);
sectionTab.transform.SetSiblingIndex(0);
}
}
}
LevelSectionTab.cs
public class LevelSectionTab : MonoBehaviour {
private LevelSection section;
private TextMeshProUGUI title;
private TextMeshProUGUI count;
private GameObject completed;
private void Awake() {
expandButton = GameObject.Find("ExpandButton");
title = GameObject.Find("Title").GetComponent<TextMeshProUGUI>();
count = GameObject.Find("Count").GetComponent<TextMeshProUGUI>();
completed = GameObject.Find("Completed");
}
public void BuildModel(LevelSection section) {
this.section = section;
title.text = section.name.ToUpper();
count.text = section.firstLevel + " - " + section.lastLevel;
completed.SetActive(section.completed);
}
}
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!!
Your answer
Follow this Question
Related Questions
Calling function to update variable from prefab script not working as intended 0 Answers
Showing a high score in the menu 1 Answer
Build a House using code and prefabs 0 Answers
TMP Dropdown resets its value 0 Answers
Why does GetComponent().SetText only change the Text mesh component of the first Prefab? 0 Answers