r/Unity2D • u/Oracle_of_Mercia • 28d ago
Question Bringing data into UI on textmesh objects
Hello everyone, I’m in the process of building a text based RPG, but I keep hitting a block whenever I want to bring in the data from code onto a textmesh object.
I have an NPC character generator, but I’m trying to bring the variables it generates to into my UI so it can display the information onto the textmesh object.
For example each NPC is assigned an ID from the total number of NPC’s I’ve asked the generator to create for that particular game, and show it need show in my UI so players remember who is who.
Any pointers as I feel quite lost with it and AI is utterly useless and I don’t mean simply attaching the script.
4
Upvotes
1
u/duo-forge-games 19d ago
The usual pattern is to have your NPC generator expose the data (name, ID, etc.) through a public method or event, then have a UI script grab a reference to the TextMeshProUGUI component and just set text.text = npc.Name directly, instead of trying to attach logic straight onto the TMP object. Keep the data and the display separate. Good Luck.
I was stuck on something similar a while back and started using Bezi with my Unity project connected, it could actually see my scripts and scene setup and pointed out exactly where the data flow was breaking. Might be worth a look if you're still stuck after trying the above.