#Text Not Updating in two places

1 messages · Page 1 of 1 (latest)

normal turtle
#

Okay so, I have two text components that display the amount of cookies that the user has. 1 for the main view where they gather them and one where they purchase upgrades.

#

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

feral ivy
#

Seems like you are storing the amount in different places . . .

normal turtle
#

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

tribal garden
#

Can you show the code that does that?

normal turtle
#

yea I am getting it rn

tribal garden
#

yeah, so in UpdateText you are only setting the text on one of your TMP text objects, not both

normal turtle
#

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

tribal garden
#

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

feral ivy
#

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 . . .

tribal garden
feral ivy
normal turtle
#

I will go the events route as I will end up using events in future projects I'm sure

#

Thanks for the support!