Disable script from code

Hi all, How do I disable script and functions from that script within code.

I've used

var script : thescript;
thescript.enabled = false;

This results in the script being disabled but the functions still work. How do I disable or disconnect that script so that it completely turns off and does not affect the game?

Thanks all for any tips on this.


Behavior.enabled, which MonoBehavior.enabled is inherited from, only switches the calling of Update() on/off, as was previously commented, and as per the documentation:


Note that MonoBehavior is a class, of which an object is instantiated; you can't disconnect it entirely in the sense that it disables the ability to call those of its methods you yourself defined.

If you want to do that, you can either check to see whether the script is enabled before your code calls one of its methods yourself, or you can enclose the content of your methods inside the script in if(this.enabled) {}.

You should use the function GetComponent:

var script : thescript; 

function Start() {
  script = GetComponent(thescript); 
  script.enabled = false;

When you disable a component that, it' just stop doing the codein Update and stuff like that, but I think it doesn't disabled your own functions. I'm not sure your can do that, maybe try scrit.gameObject.active = false;

What about Destroy(this); ?

Here’s the relevant doc link : http://docs.unity3d.com/Documentation/ScriptReference/Object.Destroy.html

use it

will works

public GameObject otherobj;//your other object
	public string scr;// your secound script name
	void Start () {
	(otherobj. GetComponent(scr) as MonoBehaviour).enabled = false;