- Home /
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.
Peter
Answer by efge · Dec 25, 2010 at 04:23 PM
You should use the function GetComponent:
var script : thescript;
function Start() { script = GetComponent(thescript); script.enabled = false; ...
Thx will try get component and report Cheers unity creators!
I've tried using this method and it doesn't work for me. Am I missing something? I get a message saying "CarUserControl" isn't a valid type. Im sure its because of the colon (:) but why did it work in your case and not $$anonymous$$es?
Here is the script.
var script : CarUserControl;
function Start () { script = GetComponent("CarUserControl"); script.enabled = false; }
Since this has been resurrected yet again it is worth stressing that while this is the accepted answer, it does not do what the OP was asking for.
See CHPedersen's answer.
Answer by CHPedersen · May 03, 2011 at 06:45 AM
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:
http://unity3d.com/support/documentation/ScriptReference/Behaviour-enabled.html?from=MonoBehaviour
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) {}.
Answer by Berenger · Dec 25, 2010 at 04:11 PM
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;
Yeah, that works fine, but it caused that the whole gameObject with all Components became false (inactive).
I've just resolved the problem! I wanted to disable script in door animation gameObject from another script (deactivator gameObject). So I wrote a code in deactivater gameObject in which I disable SphereCollider trigger (that is a collider on door gameObject), which means that the script in that door gameObject is also deactivated.
Here is the code:
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class Trigger1 : $$anonymous$$onoBehaviour {
public GameObject Player;
public Text Text;
//public GameObject doorLocked;
public Collider collider;
void Awake()
{
collider.GetComponent<SphereCollider>();
collider.enabled = false;
}
void Start()
{
collider.enabled = false;
Text.enabled = false;
//doorLocked.SetActive (true);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject == Player)
{
collider.enabled = true;
Text.enabled = true;
//doorLocked.SetActive (false);
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject == Player)
{
Text.enabled = false;
//doorLocked.SetActive (false);
}
}
}
Answer by graslany · May 13, 2013 at 04:21 PM
What about Destroy(this); ?
Here's the relevant doc link : http://docs.unity3d.com/Documentation/ScriptReference/Object.Destroy.html
disable isnt the same as destroy...the answer is already solved by the way.
Destroy can be used, then you can "re-enable" the script with AddComponent(). However I'm not sure if this is very efficient
Answer by kami1339 · Mar 28, 2019 at 12:36 PM
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;
}
No, it does not for several reasons:
First of all this question is 9 years old and was about UnityScript, not C#
The GetComponent version that takes a string argument has been deprecated and is no longer documented so it shouldn't be used anymore. The main reason is that Unity now supports namespaces and you should always use either the generic version or the System.Type version.
You use an "as" cast without null check which is a no-go. If you know that the cast will always work, use a normal cast (which is faster and produces a better error if it actually fails). If you expect that the cast may fail you can use an as cast but you have to perform a null check on the casted reference. Since in your case the user can specify any string, chances are very high that either GetComponent returns null (because misspellt name) or the component is not a $$anonymous$$onoBehaviour and the as cast returns null.
If you want a generic way to disable any $$anonymous$$onoBehaviour, just declare your variable as $$anonymous$$onoBehaviour and drag&drop the component you want to disable onto the variable directly
public $$anonymous$$onoBehaviour script;
void Some$$anonymous$$ethod()
{
script.enable = false;
}
If you don't know how to drag&drop a certain component, see my answer over here
doesent work for me
Blockquote
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class DisabledScr2 : $$anonymous$$onoBehaviour {
public $$anonymous$$onoBehaviour script;
// Start is called before the first frame update
void Start()
{
Debug.Log("e");
}
public void OnTriggerEnter2D(Collider2D node)
{
if (node.gameObject.tag == "Untagged")
{
Debug.Log("maboi youre breaking my heart");
script.enable = false;
}
}
}
If you have a problem, you're best off posting a new question, explaining what the problem is, rather than adding it as a reply to an old comment that was only there to critique someone's hole-ridden answer to an even older question.
And remember, "doesn't work" doesn't tell anyone what your problem actually is.
And take note of CHPedersen's answer to this question. Disabling the $$anonymous$$onoBehaviour does not do what the OP was asking for - Bunny's code was not intended as an answer to the OP's question.
Your answer
Follow this Question
Related Questions
Disable a script from another script 2 Answers
Disable SCRIPT HELP!!!! 1 Answer
What should I do? Disable the AI or have the AI simply be recylced and reused? 1 Answer
Disable all instances of a component 2 Answers
Re Enable C# script on camera 1 Answer