Speed performance of Update vs ScriptB

I am currently writing a mini-puzzle game for a demonstration. I have some very inefficient code, which I am working on optimizing. One thing that seems a little slow is that I am constantly looking for variable values in Update() to go from false to true and back, in order to trigger certain animations and audio. Wouldn’t it be way faster to directly call that function from the code where the variable is set? I hear GameObject.find is slow, however what about ScriptB, where I can load in a script into another script and access just that function? Would that be faster than just constantly looking for variable changes in Update? Plus there wouldn’t be the minute lag I get when 8 scripts are searching for the same variable change in Update. Thanks.

I’m not sure what ScriptB is, but in C# we use events and callbacks to accomplish this.

  • Script #1 can listen to Script #2. There are multiple ways to hook up scripts. GameObject.Find is one way and should only need to be run once.
  • Script #2 announces that a boolean has changed and provides the value. (Note, script #2 does not need to know about script #1! Very handy.)
  • Script #1 hears this event and can act accordingly.

This avoids having to check a bool every Update frame. It also separates code cleaner as Script #2 does not need to know about any other scripts in the game to function.