- Home /
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 :)
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:
Answer by Bunny83 · Apr 26, 2014 at 09:49 AM
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:
: $$anonymous$$onoBehaviour
That's not possible ;) If the class isn't derived from $$anonymous$$onoBehaviour / Component it can't be attached to a GameObject at all.
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.
@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 ?
Seems like good practice to me, if that's something you need to be able to do.
Also, to answer the question about whether or not OnTriggerEnter2D, for example, will still run even if the Behavior is disabled, it will.
I was encountering this issue and I was confused. I disabled my behaviors that had triggers in them and they continued to go off. Threw me through a loop for a $$anonymous$$ute lol
Bunny83's answer actually helped me realize why. I also have a behavior that's for handling OnDestroy and it didn't have a disable check box. I was wondering why that was and this helped me realize why disabling my script wasn't working.
Answer by eovento · Apr 08, 2019 at 09:51 AM
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.
Your answer
Follow this Question
Related Questions
Exposing Variable and Enforce Component Dependencies. 1 Answer
How to "Collapse all" components on gameobject in inspector window? 0 Answers
Extend default image script to add feature? 0 Answers
Public script references in the Inspector 2 Answers
Certain components do not update in-game until refreshed in inspector 0 Answers