#Text Not Updating in two places
1 messages · Page 1 of 1 (latest)
The issue is that when they gather cookies the amount of cookies they have isn't displayed in the upgrades menu. AND when they purchase an upgrade it updates the amount of cookies they have in the upgrades menu minus the cookies it costed to buy the upgrade so say they gathered 20 and the upgrade costed 2 the amount would update from 0 to 18 (which isn't correct as it should start out at 20 then update to 18). and on the main menu the cookies don't update until you gather them and then it is just incorrect again
Seems like you are storing the amount in different places . . .
Both text fields use the same text definition
When I update the text in one place I store it in a variable that both CookiesManager and UpgradesManager uses to set the text component's text field
Can you show the code that does that?
yea I am getting it rn
https://paste.gg/p/anonymous/1f339d08577446d3b8d6acb40d3e1125 here is all the related code
yeah, so in UpdateText you are only setting the text on one of your TMP text objects, not both
Mmm can you recommend how I should manage this more effectively then?
I dont wanna have super messy code where I reference too many things
A few ways.
1: Make a new script like "CookieText" that takes a Cookies reference and updates the TMP text component on this object to the current amount of cookies every update, attach script to both text gameobjects. Pretty inefficient but super easy to do.
2: restructure your code so changes to cookie amounts go through a central location that updates both text objects.
3: use event delegates or w/e they are called to subscribe to cookie amount change events.
in 2 you'd make something the canonical gateway for changing cookies, like the cookie manager, and have the upgrade manager try to make changes to cookies through that, that way it knows when to update both text gameobjects. something like this for instance
as opposed to what you have now which is like
Just what I thought, you have two text components but only set one of them. You could have easily use debug log to check if both of them were changed or not . . .
Easiest way is to use events (actions). Create an event. When the value is changed, invoke an event. Have both scripts listen to this event and call a method that updates the cookie text for each script. Done . . .
I prefer this way, but I think most beginners don't understand events at all. (maybe I'm wrong on that)
Then it's smth to learn. Events are the best way to have individual systems talk to each other without adding dependency. If you remove the event (and the code to access it), your scripts shouldn't break . . .