- Home /
Which way is better: Script 1 triggers script 2, or script 2 continuously checking
Hello
Lets say I have an game object with 2 scripts. One script handles a button click in which it saves a certain text ("xyz") to player preferences. The other script, update a label on screen to show the saved preference value.
Which way would be the "Correct unity way" or the better way 1- Once button is clicked, then script 1 look for script 2 in the game object and calls a method in there to tell it to read the pref value to update label
2- Script 2 has the update label code i the UPDATE method (which read from player pref and update label).
Both will do the job but I am wondering why would you go for one vs other
Answer by James2Games · Feb 01, 2017 at 05:00 AM
If at all possible it's best to avoid using the Update method when you can as this is meant for frame by frame actions. If you are able to do it by triggers then it's best to do it that way to avoid that 1 script that is constantly ticking but not doing anything.
However
If the label is knowingly being changed every frame then it's best to use Update.
Answer by AR_Rizvi · Feb 01, 2017 at 05:08 AM
I will prefer your first approach because its taking much less time and logically more better then the second approach you stated
As of Update is calling every frame it would be better to call your method on a event which is called only one time as a button pressed
So best Way is Script 1 trigger Script 2
Answer by sevic69 · Feb 01, 2017 at 09:09 AM
You might want to have a look at this, it has helped me a lot for issues like yours. http://wiki.unity3d.com/index.php?title=CSharpNotificationCenter Original Author: capnbishop CSharp Author: thedadymac