Is Update function called when it does not exist in the script?

Hello,

I’d like to know whether the Update() function gets called every frame even if it doesnt exist inside a user generated class that is derived from Monobehaviour.

I’ve read somewhere in Q&A that it isn’t called(without proper explanation), and i would like to know how this was implemented, as it sounds like a cool performance optimization.:hushed:

Cheers,

Apprentice

If you don’t have an Update function then it can’t possibly be called since it doesn’t exist.

–Eric

I think what Apprentice Is asking is how does unity know if update exists without attempting to call it. E.g. is update an empty virtual function that always gets called (its not), so does unity check if the function exists at runtime (using reflection?) for every monobehaviour and queue them up?

Karl

Isnt it bad for performance when reflection is used very often???

I’m also wondering about this, because I’m trying to decide whether to handle clickable gameobjects by attaching a script to them which just contains a function called Interact() which is called if the object is clicked on. I’m wondering whether this method will slow things down if Unity runs through all scripts in the scene every frame even if the scripts don’t have an update function. I’m assuming the lack of an update function would prevent the script from being checked even briefly by Unity, but could anyone clarify what’s going on under the hood?

Yes.

–Eric

1 Like

I would assume that Unity keeps a list of components that have Update methods on them. This list would be built on startup, and as components are added they would be added to this list if they have an Update method (and removed when destroyed). Unity’s internal game loop would then just iterate that list when it’s time to call Update on everything. Just guesswork on my part but the concept is ultimately all that matters here.

1 Like

Unity will only call functions that exist (including empty functions).

Here’s a snippet from the following blog post…

6 Likes

So what I said, only not a guess. :stuck_out_tongue:

Yes, spot-on Dave!

So to clarify, if you have an otherwise empty gameObject with one script component like this:

public class BoringMBComponent : UnityEngine.MonoBehaviour{}

then the gameObject has just the Transform component and this BoringMBComponent component.

Does this BoringMBComponent component add any performace cost at all (after compiling the game and loading the scene)? I would hope that the engine effectively decides to pretend that the gameObject is totally empty (i.e. is nothing more than a Transform object).

The reason for my clarification request is that I sometimes like to use “empty” MonoBehaviour componets as ways to filter-out an incorrect gameObject/transform of interest.

e.g. if I have a different script with the following code

public class HappyMan : UnityEngine.MonoBehaviour
{
        public BoringMBComponent myThingy;
        GameObject _myThingyGO;

        void Start()
        {
                _myThingyGO = _myThingy.gameObject;
        }
}

then in this code I can be sure that _myThingyGO is a GameObject with a BoringMBComponent. In effect, I can define a class/family of gameObjects by the predicate " gameObjects.hasComponent<BoringMBComponent> "

Update will not be called if it does not exist in a script.
Behaviour callbacks are registered before OnEnable with a manager, if they have no callback then it’s not added to the list so has no performance impact.

I was pretty close :slight_smile: