Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
9
Question by HolBol · Sep 07, 2010 at 10:03 PM · gameobjectcomponentdisableenable

enable/disable specific components

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image VoLkizz · Sep 23, 2014 at 04:30 AM 0
Share

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?

avatar image robertbu · Sep 23, 2014 at 04:32 AM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
32
Best Answer

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;
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HolBol · Sep 08, 2010 at 02:19 PM 0
Share

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

avatar image HolBol · Sep 08, 2010 at 02:33 PM 0
Share

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?

avatar image Jessy · Sep 08, 2010 at 10:26 PM 3
Share

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

avatar image
9

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;
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unfox · Dec 07, 2012 at 11:35 AM 1
Share

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;

avatar image DP90 · Feb 04, 2015 at 11:21 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges