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);
           Destroy(other.gameObject);
		Destroy(gameObject);
	}
}

And what i got in the editor:

[25761-capture+d’écran+2014-04-26+à+09.43.48.png|25761]

1 Like

Nicely written question. I sent reward points.

3 Answers

3

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.

Hard to say without seeing what you ended up with, but my guess would be that you didn't include this part in the header: : MonoBehaviour

Correct me if I am wrong, but script will not receive the onDestroy event unless one of the mentioned methods is in the script to begin with, thus [for example] adding a Start(){//do nothing} in the script may be what is needed.

@dingben: To be honest i never have tested this case so i can't tell if OnDestroy will be called or not. OnDestroy wasn't there all the time, it was added later. It's possible that they might have "missed" it. Keep in mind that it has to be spelled "OnDestroy", not "onDestroy".

@Bunny83: The same thing happened to me as well. Thanks for the explanation! :)

Even with the checkbox invisible, is it however still possible to "disable" and "enable" the component through scripting, then force said component to shutdown with statement like if(isActiveAndEnabled) ? Would this be good practice ?

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 https://forum.unity.com/threads/cant-disable-some-components-in-inspector.1395976.

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