[Solved] Performance - rewriting text.text every frame

I was wondering if there is a performance hit when Update gets called? We’re rewriting the text.text value every frame even if it hasn’t changed. I’m an experienced C# developer but I’m just starting with Unity. As a developer I don’t like reassigning a value to a variable if there is no need to do so. Does Unity optimize for this?

Thanks,
Phil

1 Like

It is indeed unecessary and it does slow down the performance. Although it won’t be to much of a problem in this case since this application is very light. I also like to avoid as much as possible running things under the Update event

1 Like

Thanks Joao, that’s good to know.

I suppose for this example, in order to keep the programming as simple as possible it can be ignored (for the moment).

I’ve taken a slightly different approach and decided to use a boolean variable that is used to signify when the state has changed, and only then do I update the text.text value.

Here is how I did it : https://gist.github.com/fatphil2001/dc8928cc1e54f458e505f81cc708cd41
I also used switch/case statements instead of if/else if

Phil

4 Likes

I agree!
Nice approach, would you mind sharing your code so other students can try it too? :smiley:

By the way @fatphil, welcome to the community, I hope that you keep sharing your experiences around here

2 Likes

I’ve just updated my previous response with a link to my code.

Hope that helps,
Phil

1 Like

Just a little twist for the @fatphil solution… It also uses a switch construction.

I have divided every state for 2 states: e.g. state_name_static and state_name_action.
So when you want to switch the state, you set it to the static variant. All the static variant have to do is make things like printing debug information and some operators. In other words, all the stuff that should be made once in that state. Then it should switch the state into action variant. Which will only wait for action from the user.