Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Maserat · Mar 08, 2013 at 09:44 PM · componentdisableadding

How to add a disabled Component

I want to add a component in its disabled state. When you add a component using addComponent the component is added and runs. I want to add a component and not have it enabled until later.

Comment
Add comment
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

4 Replies

· Add your reply
  • Sort: 
avatar image
-2
Best Answer

Answer by robertbu · Mar 08, 2013 at 10:53 PM

You can set the the enabled flag of the component to false right after you add the component:

 TestStart ts = (TestStart)gameObject.AddComponent<TestStart>();
 ts.enabled = false;

Awake() will be called, but the Start() call will be put off until the component is enabled. So you can put logic that you want to wait to execute in Start().

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 gilley033 · Jul 11, 2013 at 08:59 PM 3
Share

Unfortunately this results in OnEnable being called twice, once when the script is added and once when it is reenabled. This isn't a problem if you're not using the OnEnable method, which may be most cases, however, there is another (albeit terrible) solution.

You can add this.enabled at the end of the Awake of the script which is being added during runtime. Of course, there is a serious issue with this method, as it means all scripts of that type will be disabled automatically when created, even if they're not suppose to be. So basically you could only use this method on scripts which you ALWAYS want to start disabled, and which you had some method of enabling.

Also robertbu, I don't think you need the (TestStart) cast in the above code. I believe the generic version of AddComponent returns the type T.

avatar image guavaman · Sep 12, 2013 at 05:38 AM 0
Share

Actually this doesn't work right for colliders. They will always be enabled in the first frame and will react with other colliders around them. You set active = false on the gameobject, but this is a big problem if you want to add a collider as a component onto another object that must stay active.

avatar image
4

Answer by Narmer · Jun 09, 2015 at 12:49 PM

In TestStart's Awake function put:

 this.enabled = false;

Comment
Add comment · Show 1 · 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 pointcache · Oct 04, 2016 at 10:06 PM 0
Share

nice trick haha

avatar image
2

Answer by punk · Aug 29, 2019 at 04:39 AM

This is old, but the way I dealt with it was to add a new GameObject, then Disable it, then add my components and finally Enable it, this way all of the scripts get executed in the order you would expect and you can set whatever variables you need before Awake.

 GameObject _RuntimeComponents = new GameObject();
  _RuntimeComponents.name = "_RuntimeComponents";
  _RuntimeComponents.transform.SetParent(gameObject.transform);
  _RuntimeComponents.SetActive(false);
 
 fsmAnimator = _RuntimeComponents.AddComponent<FsmAnimator>();
 fsmAnimator.animator = animator;
  _RuntimeComponents.SetActive(true);





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
0

Answer by pointcache · Oct 04, 2016 at 10:55 PM

Thanks to @Narmer for only working solution expanding on it this is what i did : The component below is designed to work with manual initialization and normal mode in editor. Manual means the component will be added and will be disabled, awaiting the "config" to be provided, then enabled. This is achieved by @Narmer s tip in awake. However because you disable the object in awake it calls OnDisable, so you have to make sure you skip that call by using "initialized" boolean that is set true in OnEnabled. This allows for component to have manual and auto initialization.

 /// <summary>
 /// Component has to be enabled from outside, because it starts disabled for delayed initialization
 /// </summary>
 public class InputBlockerComponent : MonoBehaviour {
 
     public InputBlockerConfig config;
     /// <summary>
     /// Component will work normally if this is active, otherwise it will be disabled in awake
     /// </summary>
     public bool StartByMyself = false;
     bool initialized;
     void Awake()
     {
         if(!StartByMyself)
             enabled = false;
     }
 
     void OnEnable()
     {
         InputBlockersSystem.RegisterBlocker(this);
         initialized = true;
     }
 
     void OnDisable()
     {
         if (!initialized)
             return;
         InputBlockersSystem.UnRegisterBlocker(this);
     }
 }
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

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

15 People are following this question.

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

Related Questions

how to make my component disabled after an animation 1 Answer

disable collision on an object when pressing Q (3D) 1 Answer

How to find a component and disable it from another objects script? 2 Answers

uGUI element component names? 4.6 Beta 2 Answers

disable/enable script js 2 Answers


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