MonoBehaviour derived class out of control

Hello,

I have a class that derives from MonoBehaviour. The class is not being instantiated anywhere in code nor is it added as a component of any prefab / gameObject. In other words the code is never executed. Except it is. I put Debug.Log in Update function and in Console I see it run. Why is that?
I kept stripping the class in search of source of the mess I was getting till there is nothing in it now but before that there was a SerializedField and so it was producing massive amounts of “UnassignedReferenceException” errors. It took me hours to figure out what was going on and that’s because I had this script working for a very long time and it never caused any such problems.
What has changed since it started being initialized without any reason and beyond my control - I have no idea.

Surely I am doing something very basic terribly wrong?

To clarify an answer like “just add the script to prefab and set your serializefield like you should have and don’t worry that unity doing crazy stuff with your code behind the scene” is no good. There reason why I started my long investigation that lad me to conclusion that I don’t control how why and when my class is being instantiated is because I did have that script attached to prefab and the serialized field was set but I was getting errors anyway. When debugging the line that was causing it it would occasionally look good (serializeField had the value set in editor) but more often it would be another instance of the class (where there should be be only one I create in my code) with serializefiled being null.

Let me consult my crystal ball.

Sorry wasn’t able to see the source in this murky globe :stuck_out_tongue:

I said my class derives from MonoBehaviour and is empty other then having Debug.Log inside Update(). Will it really make a difference if I actually code the thing for you? I’m sure you’ve seen one or two of those.

EDIT:
The problem disappeared after removing another scripted prefab. It’s an entry point for entire framework with multiple files and hundreds of lines so pointless to post or explain. Nowhere in it however is the script in question referenced once. I can’t see anything that could possibly have anything to do with it either so I don’t know WTF.
Nevertheless this is so fundamental that it’s either some crazy stuff happening to me or seasoned developers know exactly what I’m talking about.

It’s some crazy stuff happening to you.

Are you passing ‘this’ as the second parameter to Debug.Log? If not, you should - then you’ll be able to click on the error in the console and Unity will ping the object that has the MonoBehaviour attached.

I tried that and it pointed to some totally irrelevant static mesh. After examination I found it to have the script in question added as component. I have no idea how that happened. It seams to be in order now.
Thank you so much.

Hippos crystal ball should have seen that one coming…

PEBKAC