6
\$\begingroup\$

I have (methink) a simple question regarding attaching scripts to a GameObject.
Is there any performance loss if I attach a script to a GameObject for each action to be performed (such as movement, attaking, interacting) instead of attaching a single script dealing with every single aspect?

I'd like to separete these things due to clearity reasons but I wonder if calling different FixedUpdate() functions would imply some kind of issue. For example: is their execution ensured to be called sequentially or they could run asincronously?

\$\endgroup\$
  • \$\begingroup\$ Don't do non-physics in fixed update. \$\endgroup\$ – user15805 Jan 23 '15 at 15:19
7
\$\begingroup\$

I'd like to separete these things due to clearity reasons but I wonder if calling different FixedUpdate() functions would imply some kind of issue. For example: is their execution ensured to be called sequentially or they could run asincronously?

Each callback of each MonoBehaviour will be called exactly once for each update loop, but you haven't any guarantee about the order (unless you force the execution order explicitly).

Is there any performance loss if I attach a script to a GameObject for each action to be performed (such as movement, attaking, interacting) instead of attaching a single script dealing with every single aspect?

Short answer: NO.

Long answer:

of course there will be a micro overhead, because a separate component need to be stored and updated, but this isn't even perceptible (maybe if you have thousands of components, and you could eventually try to benchmark it).

In any case that's definitely the goal of a component based architecture to split functionality across specific components, so I'd encourage you to do that.

Just a side (personal)note about execution order: it's true that you can force a specific update order of components, and that's sometimes is useful (rarely from my point of view). Forcing a specific update order btw, implies an implicit relation/dependency between components, that is generally not what we want if the components are truly independent.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.