Cannot enabled / disable a script component?

The “X” which is used in the components (Inspector) to enable or disable is gone for my DestroyByContact script.

Which means I can’t disable it!

Even if i don’t especially need to disable it, i’d like to know why it do that.

Thank you for reading :slight_smile:

using UnityEngine;
using System.Collections;

public class DestroyByContact : MonoBehaviour
	public GameObject explosion;
	public GameObject playerExplosion;
	void OnTriggerEnter(Collider other) 
		if (other.tag == "Boundary") { return;}
		if (other.tag == "Player")
			Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
		Instantiate(explosion, transform.position, transform.rotation);

And what i got in the editor:


1 Like

Well, that’s simply because your script doesn’t have anything that can be disabled. The enabled property only affects the calling of Start, Update, FixedUpdate and OnGUI. Most callbacks are executed anyways. Unity only shows the enabled checkbox when the script has one of the above mentioned methods.

Since you only have an OnTriggerEnter method there’s nothing to “disable” in this script. Most of the other callbacks are invoked by other components. For example all the OnCollision / OnTrigger messages are sent by a Rigidbody component, OnBecameVisible is sent by the renderer component, OnPostRender is sent by a Camera.

You can’t really disable the whole script. The only way to do something like that is either:

  • Removing the script (with Destroy)
  • deactivating the gameobject by calling SetActive(false). However that would of course deactivate everything on the gameobject.

I had the same issue: the checkbox suddenly disappeared from my script. In my case, it was not about inheriting or not from Monobehaviour (it was), the problem seems to reside on presence (or not) of a Start function. I was using Awake and removed Start method. If I create Start again, even if leaving it empty, the checkbox reappears on Inspector.

For future reference, using Unity 2022.2.5f1 I just went through all the accessible built-in methods listed here: Unity - Manual: Order of execution for event functions.

Methods that caused the enable-component checkbox to appear in the Unity Editor:

  • OnEnable, Start, FixedUpdate, Update, LateUpdate, OnGUI, OnDisable

Methods that did NOT cause the enable-component checkbox to appear in the Unity Editor:

  • Awake, Reset, OnAnimatorMove, OnAnimatorIK, OnTriggerEnter, OnCollisionEnter, OnMouseEnter, OnPreCull, OnWillRenderObject, OnBecameVisible, OnBecameInvisible, OnPreRender, OnRenderObject, OnPostRender, OnRenderImage, OnDrawGizmos, OnApplicationPause, OnApplicationQuit, OnDestroy

I assume all the OnTrigger, OnCollision, and OnMouse methods will produce the same results.

Adding a “void Start() { } // Do nothing.” line brings the enable checkbox back, if I ever need that. (Thanks, @dingben!) Any of the above group of seven methods above should work, but Start may have lower overhead than one of the update methods.

I was unable to find any documentation about this. But I did find some other methods that activated the enable-component checkbox

Thank you to the people who answered earlier! You helped me figure this out quickly!