# \[Access Modifiers\] Why aren't MonoBehaviour messages like Update() protected?

**URL:** https://discussions.unity.com/t/access-modifiers-why-arent-monobehaviour-messages-like-update-protected/548280
**Category:** Scripting
**Tags:** Scripting
**Created:** [August 25, 2014, 5:16pm UTC](https://discussions.unity.com/t/access-modifiers-why-arent-monobehaviour-messages-like-update-protected/548280 "2014-08-25T17:16:25Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![GarthSmith](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/garthsmith/32/326236_2.png) [@GarthSmith](https://discussions.unity.com/u/GarthSmith)
#### Post date: [August 25, 2014, 5:16pm UTC](https://discussions.unity.com/t/access-modifiers-why-arent-monobehaviour-messages-like-update-protected/548280/1 "2014-08-25T17:16:25Z")

</div>

Just something I’ve been curious about for some time.

Why does Unity have us type **`void Update()`** instead of **`protected override void Update()`** as I would expect?

I also find it weird that I can change the access modifiers on those functions. Eg. changing Update() from a private function to a protected function is allowed.

I’m guessing Unity is using some kind of reflection to find the private Update() function and run it. I guess I don’t know why they didn’t just give MonoBehaviour messages a protected access modifier and be done with it. There must be a reason!

---

<div class="post-metadata">

### Author: ![GarthSmith](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/garthsmith/32/326236_2.png) [@GarthSmith](https://discussions.unity.com/u/GarthSmith)
#### Post date: [September 29, 2014, 9:52pm UTC](https://discussions.unity.com/t/access-modifiers-why-arent-monobehaviour-messages-like-update-protected/548280/2 "2014-09-29T21:52:07Z")

</div>

So something came up that made me wonder this again.

I found a bunch of Reset() functions in this project because someone (me) didn’t know that [MonoBehaviour gets Reset() messages](http://docs.unity3d.com/ScriptReference/MonoBehaviour.Reset.html). Also reminds me of another bug that was caused by [Main() getting called a lot](http://forum.unity3d.com/threads/trick-start-and-update-without-start-and-update-function.252577/) too, which isn’t even in the documentation!

If these were protected functions of MonoBehaviour, the compiler would’ve told us this is a problem.

Why aren’t Reset() and Main() protected functions? I’m curious what the reason is!

---

<div class="post-metadata">

### Author: ![GarthSmith](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/garthsmith/32/326236_2.png) [@GarthSmith](https://discussions.unity.com/u/GarthSmith)
#### Post date: [September 29, 2014, 10:13pm UTC](https://discussions.unity.com/t/access-modifiers-why-arent-monobehaviour-messages-like-update-protected/548280/3 "2014-09-29T22:13:32Z")

</div>

I have a theory, I think during the build process Unity tracks what functions each MonoBehaviour has through reflection. The goal is to avoid calling a ton of empty virtual functions on every frame on every single MonoBehaviour.

I’m just grasping at straws. But I’m thinking now it’s a performance thing.

---

<div class="post-metadata">

### Author: ![Suddoha](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/suddoha/32/110358_2.png) [@Suddoha](https://discussions.unity.com/u/Suddoha)
#### Post date: [September 29, 2014, 10:39pm UTC](https://discussions.unity.com/t/access-modifiers-why-arent-monobehaviour-messages-like-update-protected/548280/4 "2014-09-29T22:39:07Z")

</div>

> [@Garth Smith](#):
>
> The goal is to avoid calling a ton of empty virtual functions on every frame on every single MonoBehaviour.

I don’t have the slightest clue about the subject, but i’ve already thought about the part i quoted from you alot. I always wondered whether they’re called or not, but rather just for my personal interest. 🙂

---

<div class="post-metadata">

### Author: ![Vipsu](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/vipsu/32/438146_2.png) [@Vipsu](https://discussions.unity.com/u/Vipsu)
#### Post date: [September 29, 2014, 10:52pm UTC](https://discussions.unity.com/t/access-modifiers-why-arent-monobehaviour-messages-like-update-protected/548280/5 "2014-09-29T22:52:44Z")

</div>

Because they are messages, and when you write say Start function/method to your Monobehavior you implement a way to receive these messages. Calling a function has some overhead, and how little that may be can add up fairly quickly when you have thousands of behavior scripts on scene calling an empty Update and FixedUpdate or OnCollisionStay methods.

if you click the MonoBehavior class on visual studio and press Go To defintion you can actually see what methods it contains. Alterantively you can check the [class reference.](http://docs.unity3d.com/ScriptReference/MonoBehaviour.html)

---

<div class="post-metadata">

### Author: ![lordofduct](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/lordofduct/32/890751_2.png) [@lordofduct](https://discussions.unity.com/u/lordofduct)
#### Post date: [September 29, 2014, 10:59pm UTC](https://discussions.unity.com/t/access-modifiers-why-arent-monobehaviour-messages-like-update-protected/548280/6 "2014-09-29T22:59:27Z")

</div>

Yes, they use reflection to get at the methods to call.

Why? It’s so that if you don’t want to receive that message, you don’t implement the method, and the message isn’t sent. This is useful because it avoids calling an otherwise empty method. No method exists, so no operation cost is incurred.

You can put whatever access modifier you want on it because you’re implementing it yourself for the very first time.

---

<div class="post-metadata">

### Author: ![Suddoha](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/suddoha/32/110358_2.png) [@Suddoha](https://discussions.unity.com/u/Suddoha)
#### Post date: [September 29, 2014, 11:18pm UTC](https://discussions.unity.com/t/access-modifiers-why-arent-monobehaviour-messages-like-update-protected/548280/7 "2014-09-29T23:18:42Z")

</div>

@Vipsu and @lordofduct  
Yes i noticed there are actually a lot of messages and that often crossed my mind when i added a new script. Thanks for clarification so far.  
But does it happen during build as supposed by @GarthSmith or during import of the scripts? It **seems** to already happen during the import into Unity.

---

<div class="post-metadata">

### Author: ![Vipsu](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/vipsu/32/438146_2.png) [@Vipsu](https://discussions.unity.com/u/Vipsu)
#### Post date: [September 29, 2014, 11:28pm UTC](https://discussions.unity.com/t/access-modifiers-why-arent-monobehaviour-messages-like-update-protected/548280/8 "2014-09-29T23:28:00Z")

</div>

Unity is constantly compiling your scripts when it detects a change, but no messages are send before you actually hit play.  
You can however call them trough Editor scripts yourself if you like

---

<div class="post-metadata">

### Author: ![lordofduct](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/lordofduct/32/890751_2.png) [@lordofduct](https://discussions.unity.com/u/lordofduct)
#### Post date: [September 29, 2014, 11:31pm UTC](https://discussions.unity.com/t/access-modifiers-why-arent-monobehaviour-messages-like-update-protected/548280/9 "2014-09-29T23:31:06Z")

</div>

Does what happen? That the method gets registered?

It’s reflection, it happens when it needs to happen. The method is reflected out when it needs to be reflected out and called. That method once reflected may be held onto for future use in a hashtable of some sort so that the reflection doesn’t have to occur each time. This is all runtime though.

---

<div class="post-metadata">

### Author: ![Suddoha](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/suddoha/32/110358_2.png) [@Suddoha](https://discussions.unity.com/u/Suddoha)
#### Post date: [September 29, 2014, 11:31pm UTC](https://discussions.unity.com/t/access-modifiers-why-arent-monobehaviour-messages-like-update-protected/548280/10 "2014-09-29T23:31:51Z")

</div>

> [@Vipsu](#):
>
> Unity is constantly compiling your scripts when it detects a change, but no messages are send before you actually hit play.  
> You can however call them trough Editor scripts yourself if you like

I know, that’s what i meant with importing, i was rather aiming at the reflection stuff. I suppose it happens during the import/compiling process instead of the building process, do you know that by any chance?  
That would actually make more sense.

---

<div class="post-metadata">

### Author: ![LightStriker1](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/lightstriker1/32/301826_2.png) [@LightStriker1](https://discussions.unity.com/u/LightStriker1)
#### Post date: [September 29, 2014, 11:34pm UTC](https://discussions.unity.com/t/access-modifiers-why-arent-monobehaviour-messages-like-update-protected/548280/11 "2014-09-29T23:34:00Z")

</div>

Frankly, they could have went with some interfaces for the method signature instead of invoking by reflection. Oh well.

---

<div class="post-metadata">

### Author: ![lordofduct](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/lordofduct/32/890751_2.png) [@lordofduct](https://discussions.unity.com/u/lordofduct)
#### Post date: [September 29, 2014, 11:35pm UTC](https://discussions.unity.com/t/access-modifiers-why-arent-monobehaviour-messages-like-update-protected/548280/12 "2014-09-29T23:35:35Z")

</div>

> [@LightStriker\_1](#):
>
> Frankly, they could have went with some interfaces for the method signature instead of invoking by reflection. Oh well.

Agreed, I don’t like the ‘message’ system as it currently stands in Unity.
