how do i enable and disable specific components on a gameobject with a script?
3 Answers
3In 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?
– BlankBlank5454don'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?
– BlankBlank5454That'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
– Jessyokay thank you. but how can put simplejson library in my project, by using above URL I am facing same error with default library which i am using ex:-system' could not be found. Are you missing an assembly reference?
– rupalitersesoftThank you for your time and your reply. Your reply is very helpful for me thanks once again.
– rupalitersesoftWay to disable all scripts on object (C#):
MonoBehaviour[] scriptComponents = someObject.GetComponents<MonoBehaviour>();
foreach(MonoBehaviour script in scriptComponents) {
script.enabled = false;
}
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;
– unfoxThanks unfox this helped me with something that was bugging me all day. Even answered a question of mine. Here's my little example based off your code: var SSAOComponentVar : SSAOEffect; function Start () { SSAOComponentVar = gameObject.GetComponent("SSAOEffect"); SSAOComponentVar.enabled = false; }
– DP90
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?
– VoLkizzBy 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.
– robertbuThese type of questions should be posted as a separate question rather than commented on a 5 year old post. But to answer your question: You'll need to put the SimpleJSON library into your project in order for it to work.
– IvovdMarel