x


enable/disable specific components

how do i enable and disable specific components on a gameobject with a script?

more ▼

asked Sep 07 '10 at 10:03 PM

Fishman92 gravatar image

Fishman92
2.4k 101 113 128

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

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;
more ▼

answered Sep 07 '10 at 10:49 PM

Jessy gravatar image

Jessy
15.6k 72 95 196

this isn't working. i put it in, but it comes up with 'unknown identifier. 'scriptname' ... how do i put it in?

Sep 08 '10 at 02:19 PM Fishman92

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?

Sep 08 '10 at 02:33 PM Fishman92

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

Sep 08 '10 at 10:26 PM Jessy
(comments are locked)
10|3000 characters needed characters left

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 :)

more ▼

answered Oct 17 '12 at 12:29 AM

ifirt456 gravatar image

ifirt456
16

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;

Dec 07 '12 at 11:35 AM unfox
(comments are locked)
10|3000 characters needed characters left

if you trying to diable javascript from C# you can make this (gameObject.GetComponent("FPSCounter") as MonoBehaviour).enabled = false; as when i am trying it enable didn't appear for me

more ▼

answered Nov 30 '11 at 08:51 AM

Arwa gravatar image

Arwa
16

(comments are locked)
10|3000 characters needed characters left

Way to disable all scripts on object (C#):

MonoBehaviour[] scriptComponents = someObject.GetComponents<MonoBehaviour>();   
foreach(MonoBehaviour script in scriptComponents) {
    script.enabled = false;
}
more ▼

answered Dec 07 '12 at 11:39 AM

unfox gravatar image

unfox
31 1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3325
x2083
x303
x215
x133

asked: Sep 07 '10 at 10:03 PM

Seen: 17454 times

Last Updated: Dec 07 '12 at 11:39 AM