Updating the Score Text every frame

In the course, the score text is updated every frame. This seems ineffective because the score will only need to be updated after the method AddToScore in GameSession.cs is called and at the start of each scene. I know that this is a relatively small game and therefore the performance won’t be too heavily impacted but it still seems like a waste of computer processing power.

When I originally attempted the challenge before seeing the rest of the video I attempted to solve this issue. I had a reference in my GameSession to my ScoreDysplay component on the Score Text object and cached the reference during Start. I had a function in my ScoreDisplay.cs that updated the text and I would call this at the beginning of when Awake and AddToScore were called. This didn’t update the text when I switched from one scene to another, because the Score Text object would change and my reference would become invalid, but with IntelliSense, I found the function OnLevelWasLoaded and by re-caching the reference to the text I was able to get it to work.

While my solution does work, it also damaged the readability of my code and it feels like there should be a better way to do this. So my question is, how could I get a reference to the score text every time a scene loads and when GameSession is not destroyed on load, or is there a completely different way to avoid setting the text every frame that I completely missed?

Hi Josh,

You are absolutely right. It is inefficient to update the text each frame. In a little game like ours, it doesn’t matter, though, because our modern computers are powerful enough to handle that. For this reason, Rick does not focus on performance optimisation.

So my question is, how could I get a reference to the score text every time a scene loads and when GameSession is not destroyed on load, or is there a completely different way to avoid setting the text every frame that I completely missed?

Regarding the issue with the text being updated each frame, you could write a method which gets called only when the text updates, e.g. when the score changes. You know exactly when that happens because your code updates the score value in specific cases.

If you are willing to rework the current code structure a bit, you could write a solid solution with a “normal” C# class. By “normal”, I mean a class which does not inherit from MonoBehaviour. I usually use the “first version” of the singleton pattern. All you have to do is to call the static Instance method of the singleton class to get the reference of the existing object/instance. If the object does not exist, the method creates a new object/instance.

If the Score Display game object is a child of a persistent parent, it does not get destroyed. However, I would not make the Score Display persistent if you opt for the aforementioned singleton. Looking for the ScoreDisplay instance every couple of minutes when a new scene got loaded is absolutely fine. You do not have to keep a reference to save an irrelevant number of method calls if it makes the code less readable/comprehensible.

I don’t know what you mean by OnLevelWasLoaded. If you mean what I think you mean, it’s a deprecated method. You cannot use it in newer versions of Unity anymore. However, you could use a delegate instead. See here.

Hopefully, this helps. :slight_smile:

Thanks!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms