- Home /
enable/disable specific components
how do i enable and disable specific components on a gameobject with a script?
I tried to disable script the same way script.enable = false, but it just clear checkbox and script continue work like before. Is this a unity bug?
By design, only certain things are disabled in a disabled script. Post as a new question 1) your script, and 2) what you are trying to accomplish with disabling.
Answer by Jessy · Sep 07, 2010 at 10:49 PM
In order to be able to "disable" something, it generally must derive from Behaviour. (Renderer is a notable exception). If you see a checkbox in the Inspector, you can enable or disable it. There are a bunch of shortcuts. For those that have a checkbox, you can do it like this:
camera.enabled = false;
For other behaviours, like your own scripts, you can do it like this, in UnityScript:
GetComponent(MyScript).enabled = false;
or in C#:
GetComponent<MyScript>().enabled = true;
this isn't working. i put it in, but it comes up with 'unknown identifier. 'scriptname' ... how do i put it in?
don't worry, got it working- how can i use this in a script on another gameobject to find a component on another gameobject and enable it?
That's a different question! You really should not ask another question in a comment, but this is covered at the beginning of the manual, so have a link.
http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Components.html
Answer by unfox · Dec 07, 2012 at 11:39 AM
Way to disable all scripts on object (C#):
MonoBehaviour[] scriptComponents = someObject.GetComponents<MonoBehaviour>();
foreach(MonoBehaviour script in scriptComponents) {
script.enabled = false;
}
Answer by ifirt456 · Oct 17, 2012 at 12:29 AM
My script says Assets/_Scripts/EnterExit.js(24,71): BCE0019: 'enabled' is not a member of 'UnityEngine.Component'. please help since I am only a modeler :)
That's because GetComponent() returns variable of type Component. You need to cast it.
Try this (substitute SphereCollider with your type of component):
var mySphereCollider : SphereCollider;
mySphereCollider = someObject.GetComponent("SphereCollider") as SphereCollider;
SphereCollider.enabled = false;
or this:
someObject.GetComponent().enabled = false;
Thanks unfox this helped me with something that was bugging me all day. Even answered a question of $$anonymous$$e. Here's my little example based off your code:
var SSAOComponentVar : SSAOEffect;
function Start () {
SSAOComponentVar = gameObject.GetComponent("SSAOEffect");
SSAOComponentVar.enabled = false;
}
Your answer
Follow this Question
Related Questions
Toggling a script on/off using the keyboard? 2 Answers
Unable to enable script 0 Answers
disable/enable script js 2 Answers
Disable/Enable Script or Add/Remove? 1 Answer
Disable SCRIPT HELP!!!! 1 Answer