In updating some score text, why should we use the Update function instead of separate functions?
I'm following the tutorials for Survival Shooter, and there's something that made me want to ask from the "Scoring points" video.
I noticed that in the ScoreManager script, the Update function is used to change the text for the ScoreText object in the hierarchy. Question is, what other benefits does it bring other than simply making text updates simpler?
In this script for my Pong clone, I have two functions that update the text for the player scores: outputPlayerGoals and outputOpponentGoals. Aside from initializing them in the Start function, they are not called by the Update function. Instead, they are called from incrementPlayerGoals and incrementOpponentGoals respectively, which are called from OnCollisionEnter in Ball.cs.
I simply feel that using the Update function to update the score text would not be an efficient approach. Or perhaps I am thinking too much of pre-optimization and not refactoring and post-optimization as I go?
There is always more than one way to do something. This is just the way that the developer did this. Usually everyone who goes through the tutorials ends up finding ways to do things differently depending on how they want to modify it.
The tutorial is also meant to show you that a game can, and usually is, comprised of several components. In this case one of the components is a Score$$anonymous$$anager
.
The Text component checks to see if you are changing the text to an identical string and if so, does nothing, so it's not terribly inefficient to replace the string in every Update call. On the other hand, it might make more sense in your code to do some additional processing when the score changes, such as play a sound effect or animate the score, in which case it makes sense to just change the text as part of this process.
That's what I figured. Changing the text itself is sufficient. But have the audio be called to play per frame? That doesn't fit.
I think you misunderstand. $$anonymous$$y example of playing audio would be when you have some sort of score manager, in which when the score changes, some additional processing might deter$$anonymous$$e whether to play a sound and what type, along with storing the new text string -- i.e., not in the Update function.